1

I want to use some type of functionality

if ( $some_condition == false )
{
    wp_die();
    // Here i want to redirect page on some URL after 10 seconds
}

I need to do it inside a plugin. Is it possible to do that?

4
  • 1
    Why use wp_die() function? why not just add in your redirect function with the 10second delay?
    – Matt Royal
    Commented Jun 12, 2014 at 11:36
  • Is your site hosted on wordpress.com. If yes, your question is off topic and should be better asked on their support forum. If you site is self hosted, please remove the wordpress.com tag Commented Jun 12, 2014 at 11:47
  • Its self hosted. removed the comment
    – Xing Lee
    Commented Jun 12, 2014 at 12:36
  • @MattRoyal wp_die() renders a specific template which might the users are familiar with. It also sends a correct status header by a given integer status code.
    – David
    Commented Jun 12, 2014 at 13:22

1 Answer 1

5

wp_die() terminates the script execution and sends itself a 500 status header (wich can be altered by the third parameter). So you need to send the Refresh header before the wp_die() call. I also recommend to print the redirect location to the body because it's up to the client to respect the refresh header.

if ( $condition ) {
    $location = 'http://pm.dev';
    $timeout  = 5;
    $message  = 'You will be redirected to <a href="' . $location . '">' . $location . '</a>';
    $title    = 'Your title';
    $status   = 303; # this might be debatable

    header( 'Refresh: ' . $timeout . ';' . $location );
    wp_die( $message, $title, array( 'response' => $status ) );
}

(I'm not sure about the correct status code.)

Note: As this code sends HTTP Headers, there must no other output sent to the browser before this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.