0

I've got a plugin which takes input through a GET request via a shortcode. The shortcode is placed on a page with the address /search, and the query used to execute this particular piece of the search is /search/?venueName=. This works fine. However, I need to rewrite it such that when a user goes to /venue/ it loads the aforementioned search.

This gist contains my original code (which failed), and Pippin's suggestion as to where I should be (also failed). I've tried everything I can think of, and it always results in either a 404 error, or redirection to the homepage (which makes NO sense). I've also tried every rule I've come up with manually through the Rewrite plugin, and the result is the same.

Any help you can provide would be greatly appreciated. Unfortunately, I'm on a deadline on this project so I'm stressing out a little about it. This piece is the last big thing needed before the initial soft launch which is supposed to happen in just under 36 hours.

1 Answer 1

0

The first important difference between your rewrite rule, and the ones that Pippin has provided is that all of his rewrite rules point to index.php. This is an absolute must, you won't get it to work any other way, all requests must point to index.php with your query vars appended.

The second important thing to note is that a rewrite must query some type of WordPress object. Every request runs a main query, a page, a single post, a category, etc..

The simplest way to solve this is to let your admin user select a page as the search page, and then point your rewrite to that page with your extra query vars appended:

function tn_rewrite_urls() {
    add_rewrite_rule(
        'venue/([^/]+)/?',
        'index.php?pagename=SELECTED_PAGE&venueName=$matches[1]',
        'top'
    );
}
add_action( 'init', 'tn_rewrite_urls' );

function tn_query_vars( $query_vars ) {
    $query_vars[] = 'venueName';
    return $query_vars;
}
add_filter( 'query_vars', 'tn_query_vars' );

In this example, you would swap SELECTED_PAGE with whatever the chosen page slug is. Also, don't forget to add your query var to the query vars array, and be sure to flush rewrites after new rules are added.

5
  • That returns a 404 error (yes, I remembered to flush rewrites... twice). However, accessing the URL directly works fine.
    – Evertiro
    Commented May 31, 2013 at 19:56
  • did you point pagename to an existing page? if it's a child page it should contain the parent/child path: pagename=parent/child
    – Milo
    Commented May 31, 2013 at 20:11
  • I did, and actually got that issue resolved. Ended up being a caching issue locally. However, now I've got another problem. This line: 'index.php?pagename=SELECTED_PAGE&venueName=$matches[1]', would indicate that $_GET['venueName'] SHOULD be accessible and set to $matches[1]. However, this doesn't seem to be the case. Thoughts?
    – Evertiro
    Commented May 31, 2013 at 20:16
  • rewritten query vars aren't available via GET, you need to use WordPress's get_query_var().
    – Milo
    Commented May 31, 2013 at 20:35
  • I figured that out ten seconds after posting my comment... and now I feel like an idiot. Thanks for the help! You're awesome!
    – Evertiro
    Commented May 31, 2013 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.