Support » Fixing WordPress » Re-direct from one WP Page to Another

  • How do I re-direct from one WordPress page to another? I also need to exit out of the page that I’m redirecting from.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Thread Starter jpizzolato

    (@jpizzolato)

    Thanks for the information. I’m not looking to always re-direct a specific page as these solutions do.

    I am coding PHP code within a WordPress Page (exec-PHP plugin) and under certain situations I want to redirect from the WordPress page to an external page or to a different WordPress Page.

    The header(‘Location: http://www.xxxxx.com’); that you would normally use in a PHP program to redirect isn’t working and neither is the exit; or die() statements.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    You’re not looking to redirect a specific page, you’re looking to redirect FROM a page when certain situations are met?

    Like what?

    It’s hard to advise with limited data 😉

    Thread Starter jpizzolato

    (@jpizzolato)

    No I am not looking to redirect a specific page. I am looking to redirect FROM a page when certain situations are met.

    I am using the plug-in exec-PHP which makes it possible to turn a WordPress page into a PHP file. So when the page is executed, say from a menu, its actually executing a PHP file. The first line of the WordPress page is actually <?php with the page being a combination of php code and html code.

    During execution of an “IF” statement I want to branch off to either another WordPress Page or an external website and then die() or exit;

    I am trying to redirect the page using the
    header(‘Location: http://www.xxxxx.com&#8217;); statement but I am getting the following error:

    Cannot modify header information – headers already sent by (output started at /home/jpwebdev/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:221) in /home/jpwebdev/public_html/wp-includes/pluggable.php on line 897

    Is there a way to re-direct the flow from the current WordPress Page or is this something you can’t do?

    WordPress also does not like the die() or exit; statement. All I get is a blank screen whenever a die() or exit; is executed.

    Thanks for your help

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    I am looking to redirect FROM a page when certain situations are met.

    During execution of an “IF” statement I want to branch off to either another WordPress Page or an external website and then die() or exit;

    From any page? That is like, if I check ‘I like cheese’ on your header, on any page whatsoever, I get sent to a cheese factory page? I’m being pedantic because I want to make sure I understand what you want, otherwise I’m stuck with broad generalities.

    On page <whatever> IF i do FOO, I am set to page <otherwhatever>?

    Any chance of sharing your code at this point?

    I would be using page templates, personally, and not PHP in the content of the page, but either should, generally, work. Also it’s wp_die() I believe.

    Thread Starter jpizzolato

    (@jpizzolato)

    Thank you for looking at it. What follows is a very simplistic example of what I’m trying to do. I got an error on the Header statement and the wp_die() also received an error. The Page name is “key-code”. If someone brings up the key-code page from the menu, I only want them to receive the codes, IF they are logged in, otherwise I want to return the WordPress Login Page and kill the process. See below

    Is there another way of transferring control from a WordPress Page to another Page or Website??

    Page “key-code”

    <?php
    global $current_user;
    if ( isset($current_user) ) {
        $UserName=$current_user->user_login;
    }
    
    if ($UserName!="")
        {
        header("location:http://www.abc.com/login");
        wp_die();
        }
    
    echo "key is 123JKP";
    echo "alternate key is 876GHI";
    
    ?>

    I received a “Headers already sent” message and there was an error on the wp_die() command:

    Warning: Cannot modify header information – headers already sent by (output started at /home/jpwebdev/public_html/wp-content/themes/Polished/header.php:2) in /home/jpwebdev/public_html/wp-content/plugins/php-execution-plugin/includes/class.php_execution.php(273) : eval()’d code on line 9

    Warning: Missing argument 1 for wp_die(), called in /home/jpwebdev/public_html/wp-content/plugins/php-execution-plugin/includes/class.php_execution.php(273) : eval()’d code on line 10 and defined in /home/jpwebdev/public_html/wp-includes/functions.php on line 2682

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Okay, a quick lesson in why it’s NOT working.

    If the browser receives the HTML tag before the script, it can’t re-apply the headers. Since you’re doing this IN the WordPress page (and this is part of why I said I’d do it in a page template), WP has already called the header in wp_header() so you’re bumping into headers already sent.

    Read http://codex.wordpress.org/Function_Reference/wp_die for how to use wp_die() by the way. I should have linked to that last night.

    Now. There are many different ways to handle this, but for my money, I’d dump the idea of a ‘redirect’ and go for a simpler ‘If logged in, show the keys. Else, show anything else’

    <?php
    if ( is_user_logged_in() ) {
        echo "key is 123JKP";
        echo "alternate key is 876GHI";
    } else {
        echo 'Only registered users may see key-codes. Please <a href="http://www.abc.com/login">login</a>.';
    }
    ?>

    Or if you MUST redirect:

    <?php
    if ( is_user_logged_in() ) {
        echo "key is 123JKP";
        echo "alternate key is 876GHI";
    } else {
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.abc.com/login">';';
    }
    ?>

    Check out the codex for more info on how to use is_user_logged_in. What you’re doing WILL work, but IMO, it’s better to use functions that already exist than re-invent the wheel.

    Thread Starter jpizzolato

    (@jpizzolato)

    Ipstenu, thank you for your support and suggestions. The code I supplied you is just an example, and a very simplistic example at that, of what I’m trying to accomplish. The actual code consists of many nested IFs because of my inability to get the dynamic re-direct to work. Aside from the Username there are other tests the program has to make.

    I will look over the documentation you suggested, as well as read up on using Page Templates to accomplish what I’m trying to do. Can you suggest a good documentation on how to use Page Templates for this purpose?

    Thanks again.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    http://codex.wordpress.org/Pages#Page_Templates is a basic start 🙂

    Though do consider that you may be reinventing the wheel. I know you have a lot of conditionals, but if you spell them out, we may know a plugin that already does all that (like Members – http://wordpress.org/extend/plugins/members/ – That may be what you need.)

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Re-direct from one WP Page to Another’ is closed to new replies.