0

I am having an issue with redirect rules for a single-page app that is on a sub-page of a Wordpress site. I have followed this set of instructions pretty directly and am still having issues: Redirect sub-pages to parent without changing URL

The subpages are custom post types for business locations. When someone visits http://business.com/hollywood-ca/contact it should pull up http://business.com/hollywood-ca/ but the url needs to remain the same (The contact portion of the URL is part of a single-page Vue.js app on each location page, so it needs to stick around). Here is my code:

//This function hooks in on post save
function add_location_deep_links( $post_id, $post, $update ) {

    $post_type = get_post_type($post_id);

    if ( "location" != $post_type ) return; // If this isn't a 'location' post, don't update it.

    $slug = $post->post_name; //hollywood-ca

    add_rewrite_rule(
        "^{$slug}/[A-Za-z]",  //regex prevents trying to redirect the /hollywood-ca/ page
        "index.php?page_id={$post_id}", //redirect to post
        'top' // take precedence over other redirects
    );

    flush_rewrite_rules();
}

The problem is when I visit http://business.com/hollywood-ca/contact the page redirects to http://business.com/hollywood-ca/ which prevents my single-page app from navigating to the contact tab.

If it helps, I have also written a couple of functions that change my URLs from business.com/location/hollywood-ca to the cleaner business.com/hollywood-ca. I have tested these issues without those functions and am still having issues, so I don't think they are related.

  • page_id is only for the built in page post type. what is your CPT's query var? that's what you need to set. – Milo Oct 14 '16 at 18:37
  • I didn't set a custom query var so I assume it is location, what would be the change I need to make? – Jeff Oct 14 '16 at 18:40
  • I wouldn't use add_rewrite_rule and flush_rewrite_rules with the save_post hook, but in init hook with $matches return by the regex. Just a point of view. – Benoti Oct 14 '16 at 19:07
  • My understanding is that flush_rewrite_rules is a labor-intensive function (regenerating the .htaccess file) and should ideally only be called when something changes. – Jeff Oct 14 '16 at 19:08
  • I don't really think that something changes, on save_post, you update an ID or create it. Your are not creating a new rule? For me, add_rewrite_rule must manage every new {$slug} according to the query, only one rewrite rule for your all your schema. Maybe I'm wrong but if you have a lot of post your options table (rewrite_rule key) will contain all your slug rules as I'm understanding your code. I'm missing something ? – Benoti Oct 14 '16 at 19:59
0

Milo's comment got me there:

add_rewrite_rule(
    "^{$slug}/",  
    "index.php?location={$slug}", //changed query var to location
    'top'
);
| improve this answer | |

Your Answer

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

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