WordPress.org

Ready to get started?Download WordPress

Forums

How do you "go to" a page base on a conditional? (8 posts)

  1. george@piccexcellence.com
    Member
    Posted 6 months ago #

    Sorry, this might be wordy.....
    Before converting to wordpress I used the php function "header" to move back and forth between pages. example: header("Location: " . $returnUrl . "?errorResponse=IncorrectPasssword"). This doesn't seem to work with wordPress. My question is how do you go back to the login page if id or password is wrong? This is a simplified example. I use the header() function quite a bit as a response to conditionals and need to find some way to get it to work consistently or replace it. One curious thing is that if I turn on the output buffering it works on my hosting server, however it won't work on my local machine.

    Any help would be appreciated.
    George

  2. Christian1012
    Member
    Posted 6 months ago #

    $returnUrl= "http://wordpress.org/support/topic/how-do-you-go-to-a-page-base-on-a-conditional";
    
    if ( current_user_can( 'manage_options' ) )
    	header('Location:' . $returnUrl );

    If you put that in header.php before you output anything to the browser, it should work. Does for me.

  3. Christian1012
    Member
    Posted 6 months ago #

    Re-read the question.

    If you're using ob_start() and ob_end_flush or ob_get_clean and it runs on your server, but not on localhost, your php.ini may have output_buffering turned off. Check that as well.

  4. george@piccexcellence.com
    Member
    Posted 6 months ago #

    Thanks, I'll give them both a shot...

  5. george@piccexcellence.com
    Member
    Posted 6 months ago #

    Hi Christian,
    switching on output buffering in php seems to have done the trick. Thanks. Never would have thought of that....should know better...

    George

  6. Christian1012
    Member
    Posted 6 months ago #

    The reason is because you can't send headers once output has started. Output buffering holds the output until you... well tell it to output. For example:

    <?php
      // start output
      ob_start();
    
      echo 'hello world';
    
      header('Location: http://www.google.com');
    
      ob_get_clean($html);
    
      print $html;
    
    ?>

    That would work, because even though you echo'ed something out BEFORE you send new headers, the output buffer didn't really send it to the browser until you printed the contents of the buffer. However, if you do this:

    <?php
      echo 'Hello World';
      header('Location: http://www.google.com');
    ?>

    It will echo something to the browser before you send the headers, which thus, won't work.

  7. george@piccexcellence.com
    Member
    Posted 6 months ago #

    Ok, question, should ob_get_clean() follow every header() call? I've had a problem with printing certificates (we provide online medical training and part of the process is printing certificates of completion). I added the ob_clean() to the printing code to get them to print otherwise the certificates would be garbled.

    wouldn't mind you direct email. I am a one person shop and would love to have someone that knows what they're doing to bounce the occasional problem off of...I've worked my way through some fairly complex code but occasionally I hit a wall....

    thanks again,
    George

  8. Christian1012
    Member
    Posted 6 months ago #

    It depends on the context George. But my blanket answer is you should "flush the contents" of the output buffer AFTER you send header(). A more appropriate statement might be, DON'T flush the contents of the output buffer BEFORE you send header(). I hope that makes sense. If not, or you'd like to read up on it, start here.

    As far as I use output_buffering, I start the output buffer with ob_start();. Then I put in all my html, php, javascript, whatever. When I'm finished, I get the contents of the output_buffer with ob_get_clean($content). Now all of the clean code is stored in the variable $content. Furthermore, ob_get_clean will erase the output_buffer I created, so I can use another one further on in the code. Whenever I want, I can echo $content; to the browser, or return $content if I'm inside a function/class. Hope that helps.

    And also this.

Reply

You must log in to post.

About this Topic