WordPress.org

Ready to get started?Download WordPress

Forums

Re-direct from one WP Page to Another (10 posts)

  1. jpizzolato
    Member
    Posted 2 years ago #

    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.

  2. jpizzolato
    Member
    Posted 2 years ago #

    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.

  3. 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 ;)

  4. jpizzolato
    Member
    Posted 2 years ago #

    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'); 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

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

  6. jpizzolato
    Member
    Posted 2 years ago #

    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

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

  8. jpizzolato
    Member
    Posted 2 years ago #

    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.

  9. 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.)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.