Suppose I have these WordPress pages:
page-Apage-Bpage-C
and a published page page-main.
My question:
The clients see and access only page-main which is in the end virtual – it doesn’t exist. Accessing this page-main page should redirect to one of the above mentioned pages A-C, depending on some conditions which are not important for this question.
How can I achieve it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
So you need this to happen before WordPress has output anything at all, otherwise you won’t be able to send a redirect.
A way to achieve this is with the template_redirect hook, documented here: https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
The example from that docs page is more or less exactly what you want. I’ve edited it a little bit for your question:
function my_page_template_redirect() {
if ( is_page( 'page-main' ) ) {
if ( $some_condition ) {
wp_redirect( home_url( $pageA ) );
} else {
... other caeses here with redirects to other pages
}
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
You’d need to add that to your functions.php or a plugin.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0