Refresh page once on page load

Hi, I have performed some speed optimisation on my site https://magicwedding.co.uk and noticed that everything loads fine if I refresh the page once after its loaded. What I would like to do is add some code to do this.

I found the following online, which states it needs to be added into the header.php:

(function()
{
  if( window.localStorage )
  {
    if( !localStorage.getItem( 'firstLoad' ) )
    {
      localStorage[ 'firstLoad' ] = true;
      window.location.reload();
    }  
    else
      localStorage.removeItem( 'firstLoad' );
  }
})();

</script>

My question is how do I add this the the header.php file? Thanks

Hi There,

Thank you for writing in, you don’t need to edit directly the header.php file for adding that script on the <head> of your site. You can utilize the do_action('wp_head'); hook instead. See more details and example here.

Your script is missing the opening <script> tag, be sure to add that

Then add your complete code to the Child theme’s functions.php file.

How To Setup Child Themes

Hope it helps,
Cheers!

Thanks for the reply. So to clarify I need to add the following code to my child theme’s functions.php file? As follows? I’ve tried this but it doesn’t seem to work?

<?php

// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to X in this file.
// =============================================================================

// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
//   01. Enqueue Parent Stylesheet
//   02. Additional Functions
// =============================================================================

// Enqueue Parent Stylesheet
// =============================================================================

add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );



// Additional Functions
// =============================================================================

function hook_refresh() {
    ?>
        <script type='text/javascript'>

			(function()
			{
  			if( window.localStorage )
  			{
    			if( !localStorage.getItem( 'firstLoad' ) )
    			{
      			localStorage[ 'firstLoad' ] = true;
      			window.location.reload();
    			}  

    			else
      			localStorage.removeItem( 'firstLoad' );
  			}
			})();

		</script>
    <?php
}
add_action('wp_head', 'hook_refresh');

Hi There,

Can you please try this code.

function hook_refresh() {
    echo '<script type='text/javascript'>

			(function()
			{
  			if( window.localStorage )
  			{
    			if( !localStorage.getItem( 'firstLoad' ) )
    			{
      			localStorage[ 'firstLoad' ] = true;
      			window.location.reload();
    			}  

    			else
      			localStorage.removeItem( 'firstLoad' );
  			}
			})();

		</script>';
}

// Add hook for front-end <head></head>
add_action('wp_head', 'hook_refresh');

Hope this works for you.

Thanks

Thanks for the reply, I just replaced the previous code with the code you suggested and got this error

Hi @kevgregson

Please try this code instead:

function hook_refresh() {
    echo '<script type="text/javascript">

			(function()
			{
  			if( window.localStorage )
  			{
    			if( !localStorage.getItem( 'firstLoad' ) )
    			{
      			localStorage[ 'firstLoad' ] = true;
      			window.location.reload();
    			}  

    			else
      			localStorage.removeItem( 'firstLoad' );
  			}
			})();

		</script>';
}

// Add hook for front-end <head></head>
add_action('wp_head', 'hook_refresh');

It’s recommended to connect to your site via FTP and do all the files editing from there, so in case something wrong happened you can quickly correct it by reverting back to its original state.

Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.