1

For our website, I've built out some "pages" using URL rewriting, with some code that I found here. It worked to have two of these special pages, but it's not working when I try to add a third. I've had to change one of the queries, and I'm not sure why. I've included the code below.

If I use /find/, I can get to the product-search.php page and go from there, but to make that work, I had to do a different check than for my /sell/ and /quote/ pages. Thinking that it might be because "find" may be used elsewhere in WordPress, I've tried many other terms, including the current "gobble," which gives me a "Page not found" error.

Figuring I'd just make them all work the same way, I tried converting the "sell" and "quote" pages to this same method, but when I did, they gave me page-not-found errors.

I'm perplexed as to why I can get both of these methods to work, but only for some of the pages. Any explanation or help so that I can use one method would be most appreciated. These are just the first few of these custom pages that we need, and I'd like to be able to be consistent in adding them.

ETA: I just noticed that the "find" page as it is implemented is giving a title of "Page not found" as well. That is definitely not ideal.

add_filter( 'query_vars', 'trade_in_rewrite_add_var' );
function trade_in_rewrite_add_var( $vars ) {
    $vars[] = 'find';
    $vars[] = 'sell';
    $vars[] = 'quote';
    $vars[] = 'gobble';
    return $vars;
}
function add_trade_in_rewrite_rule() {
    add_rewrite_tag( '%find%', '([^&]+)' );
    add_rewrite_rule(
        '^find/([^/]*)/?',
        'index.php?find=$matches[1]',
        'top'
    );
    add_rewrite_tag( '%sell%', '([^&]+)' );
    add_rewrite_rule(
        '^sell/([^/]*)/?',
    'index.php?sell=$matches[1]',
    'top'
);
add_rewrite_tag( '%quote%', '([^&]+)' );
add_rewrite_rule(
    '^quote/([^/]*)/?',
    'index.php?quote=$matches[1]',
    'top'
);
add_rewrite_tag( '%gobble%', '([^&]+)' );
add_rewrite_rule(
    '^gobble/([^/]*)/?',
    'index.php?gobble=$matches[1]',
    'top'
);
}
add_action('init', 'add_trade_in_rewrite_rule');
add_action( 'template_redirect', 'trade_in_rewrite_catch' );
function trade_in_rewrite_catch() {
    global $wp_query;
    if ( $wp_query->query_vars['name'] == "find"  ) {
        include ( get_stylesheet_directory() . '/product-search.php');
        exit;
    }
    if ( array_key_exists( 'sell', $wp_query->query_vars ) ) {
        include ( get_stylesheet_directory() . '/sell.php');
        exit;
    }
    if ( array_key_exists( 'quote', $wp_query->query_vars ) ) {
        include ( get_stylesheet_directory() . '/quote.php');
        exit;
    }
    if ( array_key_exists( 'gobble', $wp_query->query_vars ) ) {
        include ( get_stylesheet_directory() . '/product-search.php');
        exit;
    }
}
  • Have you flushed rewrite rules after adding new ones? The problem with this method is that it short-circuits the load process after an unsuccessful main query. I would look around rewriting questions here for other strategies, like using a page as the rewrite rule target, or simulating a page object dynamically, so the whole load process can happen successfully. – Milo Jan 26 '16 at 22:36
  • I have been flushing the rewrite rules each time.As I'm thinking more about this, I suspect I can do the search page as a custom page, which I couldn't do for the others. The "find" page will have GET variables, while the other pages have things like /sell/product-name/1234, with varying depths, but the "find" should always be the same. So that sets me okay for the "find" page, I think, but I hadn't found another way to do the "sell" and "quote" pages, since they need the extra stuff on the end. Anything particular to look at? Current thought is to leave it alone since it works. :) – llhilton Jan 27 '16 at 14:08
  • Do the others actually work though? If you're not ending up with a successful main query, you may see your content, but the wrong status code header is being sent. I suggest one of the two methods I mention in my first comment, I've answered many rewriting and endpoint questions here with working examples if you look around. – Milo Jan 27 '16 at 16:19
  • The /sell and /quote ones with the array_keys check do work. I get a 200 response, in addition to the page content. As I've worked on this more, I think the issue is that the rewrite rule expects something after sell, like /sell/stuff. If I just go to /sell, I get a 404 error. (Which is fortunately consistent with what we want to happen.) I was testing out the /find page with just /find. But since that's all I need, I am doing a custom page template, which is working great. Thanks for your help! – llhilton Jan 28 '16 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.