• Josh Levinson

    (@joshlevinson)


    I am developing a plugin, the goal of which is to allow for a bookmark-able, pretty permalink search engine on a single page. To do so, I am using WP’s rewrite rules to allow for the searching/permalinks.

    I need a structure as follows:
    https://example.com/courses/nc/charlotte
    Where /nc/ (or whatever is inputted/sent) would be accessible as a state variable later on, and the same for /charlotte/ as a city.

    My code so far is as follows:
    https://pastebin.com/PNHudKg5

    Currently, the echo ‘<h2>’.$course->post_title.'</h2>’; works as it is outputted before the rest of WP loads. However, if I move the code from the query parse function to a shortcode, the query params /nc/charlotte disappear off of the url and are inaccessible by my shortcode.

    Am I missing some function that will keep the query parameters so I can use them on page? It seems like when the page loads, it strips the url, but before the page loads, they are still accessible.

    Or maybe I’m doing it wrong altogether? I’ve found so many different ways of using WP’s rewrite rules that my head is spinning. WP provides documentation on the functions, but not how to hook them all together to build something with them.

    Also, I’m using Jan Fabry’s Rewrite Analyzer plugin. When I type in https://example.com/courses/nc/charlotte, I get the correct match in the window below. So I know I’ve got something right.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t see what you’re trying to do by parsing the query from within a shortcode. For a shortcode to be processed, some query has already been parsed in order to retrieve the content containing the shortcode. You could start your own query, but as you have discovered, based on what?

    I don’t have direct experience with the Rewrite API, but my understanding of it differs from your interpretation. One of us is confused, and I’m allowing it may be me, but in case it helps, here’s my take.

    You don’t need to parse the rewritten parameters, the rewrite system knows how to parse the parameters into the correct query vars. What the rewrite system needs done for it is the creation of the proper query based on those query vars. You may want to do something like use the ‘pre_get_posts’ action to construct the appropriate query.

    This will result in the loop showing the appropriate results. I still do not see how shortcodes would enter into the picture here. Anyway, I hope this helps.

    Thread Starter Josh Levinson

    (@joshlevinson)

    Thanks for the offer of help!

    The shortcode was just a temporary means of executing code on the page. I actually need the page to utilize it’s normal query in that it pulls the regular page content. The search results are supposed to be appended after the page content (hence the shortcode, or probably later – a page template).

    Therefore, I cannot REPLACE the query, I am building a new one, and adding the matched posts to the page in the area designated (again, currently a shortcode in the content; later will be a page template).

    My setup works awesome currently as I have it setup to use typical GET requests – like example.com/courses/?state=nc&city=charlotte

    The above url gets posts with meta where custom fields (in this case, state and city) match the city and state in the GET request.

    My goal is to move this functionality to WP Rewrite rules, like I mentioned above. Hope that makes it more clear.

    Moderator bcworkz

    (@bcworkz)

    OK, I think I understand now, the shortcode bit had me confused.

    If the current URL parameter setup works properly, you just need to get the Rewrite API to take permalink style URLS and convert the request into the current URL parameter style. Nothing else needs to be done (except the template in place of shortcode of course). No need to parse URLs or build queries or anything, just use your current setup.

    Thread Starter Josh Levinson

    (@joshlevinson)

    Now you got it ??

    I was under the impression that my code was the only way to do it. Any better/simpler suggestions?

    Thread Starter Josh Levinson

    (@joshlevinson)

    This thread has a reply to it, which I is why I think it may be due/valid for a bump, as I read that the rules for bumping are most typically set so that posts without replies are prioritized.

    bcworkz, or anyone else – why aren’t my query vars sticking around?

    Moderator bcworkz

    (@bcworkz)

    Too bad you didn’t get any other suggestions:( I too was curious to see any other ideas.

    I still think the best approach is have the rewrite API take an URL such as example.com/courses/nc/charlotte and rewrite it to example.com/courses/?state=nc&city=charlotte, which you say your current system handles fine. Job done.

    Thread Starter Josh Levinson

    (@joshlevinson)

    Well, the thing is… Even that logic fails. By “my system works”, I meant it works with typical GET params, like example.com/courses/?state=NC&city=Charlotte
    It doesn’t work with /nc/Charlotte.

    I’d be happy if it rewrote the friendly to the get, but I’d much rather keep it friendly all the way through.

    Moderator bcworkz

    (@bcworkz)

    I’d be happy if it rewrote the friendly to the get

    Your rewrite function alone should be achieving this, then you would at least have a functioning friendly permalink facility, though perhaps not ideal. I don’t think this rewrite appears in the user’s address bar, so from the user experience POV, it is identical to the friendly all the way through version.

    For friendly all the way through, we are back to the original problem, the query vars disappear. Unfortunately, my experience in this area is limited. I know enough to assure you you are on the right track in general, but not enough to have a clue where the problem lies.

    You may be forced to do an in-depth trace through all the code to pinpoint where things go wrong. If the rewrites are not at least delivering a functional get request, that may be where the problem lies.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘WordPress Custom Rewrite Rules’ is closed to new replies.