Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
That returns a 404 error (yes, I remembered to flush rewrites... twice). However, accessing the URL directly works fine. –  Ghost1227 May 31 '13 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 May 31 '13 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? –  Ghost1227 May 31 '13 at 20:16
    
rewritten query vars aren't available via GET, you need to use WordPress's get_query_var()‌​. –  Milo May 31 '13 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! –  Ghost1227 May 31 '13 at 20:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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