'How to redirect to another page after n seconds in wordpress without using javascript?

Following problem: I check a condition for a plugins' page in php and if it's false I want to redirect to another page.

With PHP it would simply be:

header('Refresh: 10; URL=http://yoursite.com/page.php');

if you want to redirect after 10 seconds. But in Wordpress the error "headers already sent" will occur.

The wordpress function for it would be:

exit( wp_redirect( admin_url( 'admin.php?page=your_page' ) ) );

but there is no time parameter for this function.

I know how to redirect with javascript but I'd rather do it with php/wordpress functions. Is there a way to do this?



Solution 1:[1]

You will have to be able to modify the <head> section of the document somehow and add the following meta tag:

<meta http-equiv="refresh" content="10; URL=http://yoursite.com/page.php">

This will be different depending on how you have your templates structured.

The general solution to modifying the head section in wordpress (not by using templates) is to add a wp_head action:

<?php

add_action('wp_head', 'some_function');
function some_function() {
    if($someCondition) { ?>
        <meta http-equiv="refresh" content="10; URL=http://yoursite.com/page.php"> <?php
    }
}

Solution 2:[2]

php code is server side, it is processed then passed to clients.

easy workaround:

use a special header and output the normal html redirect header

Solution 3:[3]

Ok, it seems there is no easy PHP solution for that problem so I created a javascript solution. Seems to be the easiest way to handle the redirect:

if($my_condition == false){ ?>
    <script type="text/javascript">
       $(document).ready(function() {
           var redirection_link=<?php echo json_encode(admin_url( 'admin.php?page=my_plugins_settings_page' )); ?>;                
           alert('Please check your Settings first!');
           window.location.replace(redirection_link);
        });
    </script>
<?php
}

Solution 4:[4]

This code need to be on the header you can make an extra header-extra.php and put this code inside

in the page you want be redirecte add in get_header('extra'); the variable $dw_attachment_url is the url, image id or whatever you want to be redirected

header-extra.php

  <head>
<meta http-equiv="refresh" content="10;url=<?php echo $dw_attachment_url ?>" />
<script>
    var seconds = 0;

    function displayseconds() {
        seconds += 1;
        document.getElementById("secondsdisplay").innerText = "This page will be redirect in " + seconds + " seconds..";
    }
    setInterval(displayseconds, 1000);

    function redirectpage() {
        window.location = "<?php echo $dw_attachment_url ?>";
    }
</script></head>

page.php

get_header('extra');

Solution 5:[5]

No there is no way to do it after n seconds once PHP has sent the headers (in this case the whole page).

A possible wrong option would be to let PHP hold the rendering for a while using Sleep(5) but not a good option.

Best Solution: Override the function "wp_redirect" and set an additional parameter like this

wp_redirect($page, $interval = 20)

Make sure to set the $inverval = 20 otherwise other scripts using this function might fail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 vico
Solution 3 user2718671
Solution 4 Fernando
Solution 5 DJ'