Skip to content
Advertisement

Set timeout in WordPress hook

I need help here.

I have taken 1-2 days to figure out how to redirect a user from one page to another page AFTER a few or 10 secs

I know there is sleep() function I need to be fixed but where, I am not sure about it:

I have a sample code here

function redirect_page() {

     if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
        }
        else {
        $protocol = 'http://';
    }

    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);
        echo $currenturl_relative;

    switch ($currenturl_relative) {
    
        case '[from slug]':
            $urlto = home_url('[to slug]');
            break;
        
        default:
            return;
    
    }
    
    if ($currenturl != $urlto)
        exit( wp_redirect( $urlto ) );


}
add_action( 'template_redirect', 'redirect_page' );

I am awaiting a response

Thank you in advance

Advertisement

Answer

You can use the PHP sleep() function to delay execution of a script.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement