If a page does not exist, include a different page?

I’m setting up a website for a client where they want several different sections, like so:

Regular Site (/)
  - Section 1 (/section_1/)
  - Section 2 (/section_2/)
     ... 
  - Section X

The regular site will have all of the main content, but I want to be able to override certain pages if they exist in the specified section.

Read More

Let’s say the user visits domain.com/section_1/features. If a /section_1/features page exists, I would like to display that; however, if the page does not exist, I would like it to display /features without redirecting or changing the URL in the address bar. It should be completely seamless and behind the scenes.

I think I need to add a function to the parse_request hook, something like:

function check_section_page( &$wp ) {
    $requested_path = $wp->request;

    if ( ! get_page_by_path($requested_path) ) {
        $new_requested_path = preg_replace("/^(.*?)//", "", $requested_path);
        // do something here to make it read $new_requested_path
    }
}

add_filter("parse_request", "check_section_page");

It runs the function properly, and echoing out $new_requested_path shows that it does have the new required page. I’ve been fighting with this for about 2 hours and my head’s done in. Help?

Related posts

Leave a Reply

1 comment

  1. I managed to solve the problem. Here’s how to do it, in case anyone else ever needs this solution.

    ** Updated Mar 10 3:30am EST **

    The previous function that I had here was displaying the page properly, but was returning a 404 error code. I’ve replaced the bad code example with a working one that does not return a 404.

    function check_section_page( $posts ) {
    
        if (is_admin()) { return $posts; }
    
        global $wp, $wp_query;
    
        $requested_path = $wp->request;
    
        if ( $requested_path == "" ) { return $posts; }
    
        preg_match("/^([a-z-]+)//", $requested_path, $matches);
    
        if ( count($matches) > 1 ) {
            if ( get_page_by_path( $matches[1] ) ) { $section = $matches[1]; }
        }
    
        $wp_query->set("saleslink_section", $section);
    
        $this_page = get_page_by_path( $requested_path );
        $new_requested_path = preg_replace("/^(.*?)//", "", $requested_path);
        $requested_page = get_page_by_path($new_requested_path);
    
        if ( $requested_page->ID and ! $this_page->ID ) {
    
            $posts = NULL;
    
            $posts[] = $requested_page;
            $wp_query->is_page = true;
            $wp_query->is_singular = true;
            $wp_query->is_home = false;
            $wp_query->is_archive = false;
            $wp_query->is_category = false;
            unset($wp_query->query["error"]);
            $wp_query->query_vars["error"]="";
            $wp_query->is_404=false;
    
        }
    
        return $posts;
    
    }
    
    remove_filter('template_redirect','redirect_canonical');
    add_filter("the_posts", "check_section_page");
    

    The remove_filter line will turn off the automatic-url-guessing-redirect behaviour of WordPress, when a requested URL does not exist and a similar one does and it redirects to the similar URL.