Suppose I have these WordPress pages:

  • page-A
  • page-B
  • page-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?


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.

  • Unfortunately this works only as long page-main does physically exists as a WordPress page. If page-main doesn't exist then is_page() doesn't trigger. Any idea or should I create a dummy page with the name page-main. Jul 23 '20 at 22:10
  • I upvoted and accepted your answer as it works. I create a dummy page-main in order is_page() triggers. May be you have a solution when page-main doesn't exist but it's not that important. THX! Jul 23 '20 at 22:27
  • @PeterVARGA I've been meaning to find how to do this for sometime. I'll figure it out and update the answer
    – mozboz
    Jul 24 '20 at 10:23

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.