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.
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?
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.
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.