How to Redirect On Refresh WordPress Page » Detect Page Refresh PHP

Detect Page Refresh - Redirect on Refresh WordPress PageWhen you are creating WordPress page or post redirects one of the most common need is to redirect a page after the user refreshes the page. In a recent WordPress plugin development task, I needed to create a multiple steps on-boarding user verification plugin that required a redirect on refresh for the last step of the on-boarding process.

Obviously there are several ways you can create a redirect after a page is refreshed but on WordPress I found it was too complicated to use the jQuery as follows:

$(window).bind('beforeunload',function(){

     //save info somewhere

    return 'are you sure you want to redirect since you have refreshed the page?';

});

In situation where I can use JavaScript, I prefer it to PHP since I find it easier to manipulate things using the DOM

Detect Page refresh jQuery

If you would like to use jQuery to detect page fresh the code snippets below can be very useful :

$('body').bind('beforeunload',function(){


    //do something


 });

or you can use the code snippet I shared earlier.

Detect Page Refresh PHP

In my specific case, I needed to detect the page refresh using PHP rather than JavaScript. You can achieve this using the following code snippet in PHP :

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';


if($pageWasRefreshed ) {

  //do something because page was refreshed;

} else {

  //do nothing;
}

This code snippet detects if the page has been refreshed using the F5, Right-Click or Reload,and if it is refreshed you can conditionally add your logic to the IF statement

Using Cookies to Detect Page Refresh PHP

We can also use cookies in PHP to detect page refresh as seen on this code snippet :

<?php


    session_start();
    if (!isset($_SESSION["visits"]))
        $_SESSION["visits"] = 0;
    $_SESSION["visits"] = $_SESSION["visits"] + 1;


    if ($_SESSION["visits"] > 1)
    {
        //you refreshed the page!
    }
    else
    {
        //nothing to do here!
    }

Redirect on Refresh WordPress Page

To implement the redirect on refresh in WordPress we need to use the PHP approach and add this code in an action hook and give the hook an appropriate priority. So we need to do these basic steps  :

  • Get the page ID of the current page
  • Conditionally check if we are on the page
  • Check if the page was refreshed
  • If the page was refreshed redirect to the destination page using the wp_redirect() function

So I put together these steps into the code snippet below :

// Hook into the head

add_action( 'wp_head', 'njengah_detect_page_refresh' );

//Callback function

function njengah_detect_page_refresh(){
   
//Get the page id
    global $post;

    $postId = $post->ID;

     //Check if we are on that page
     if($postId== 10 ){
        
       //Check if the page was refreshed
       
        $pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
        
       //Redirect to another page if the page was refreshed
        
        if($pageWasRefreshed){

            //redirect to homepage
            wp_redirect( home_url() ); exit;

         }else{

            //do nothing

         }

     }

}

In this code you should replace the page ID $postId== 10 with the respective page IDs that you want to test this condition. In my case I was testing on a page with the ID of 10.

Comments are closed.