How to add a delayed redirect for 1 page in WordPress

I have one page that a client wants to display for 30-60 seconds and then redirect to another page. Back in the day, I would add a <meta http-equiv=”Refresh” content=”10;url=http://www.mynewurl.com“> to that page's header but I'm not finding the ability to insert a meta tag into a single page in the WordPress setup for this site. It is using Divi and Toolset.

Divi allows for some site-level header scripts, but I'm not seeing a place to do that per page.

I also tried to use WordPress fields, but that didn't work either. And Toolset is a little clunky.

I'd really like to do this without adding another plugin for this one thing.

1 answer

  • answered 2021-06-19 08:10 David

    In your functions.php, or where you can add snippets

    add_action('wp_footer', 'web_earth_add2footer');
    function web_earth_add2footer(){
        if( is_page( 111 ) ) { // change 111 by the id of your page
            echo '<script>';            
            echo  'setTimeout(function(){';
            echo      'window.location.replace("http://www.mynewurl.com");'; // The new url you want to redirect to
            echo  '}, 30000);'; // time before redirect in millisecondes
            echo '</script>';
        }
    };
    

    This will add this javascript code to the footer of the website only on the page you need.