Add meta tag with page creation time in WordPress – for cache testing

Add Meta Tag With Page Creation Time – For Cache Testing

The best way to check whether caching is working is to print the page creation time as a meta tag in the header of every page. Caching is working if the value does not change after successive requests. Otherwise, it is not working.

For the snippet below to work, you must set your website’s Timezone correctly. Go to Settings -> General and set the Timezone by choosing a city. If you select a city as a timezone, it will save to the “timezone_string” option, but if you choose a manual offset (UTC+1, for instance), it is saved to the “gmt_offset” option (as “1”), and timezone_string remains empty.

Go to Settings -> General and set timezone to a city in the same timezone as you
Go to Settings -> General and set Timezone to a city in the same Timezone as you.

PHP Snippet: For cache testing – Echo meta tag with page creation time in the page header.

add_action( 'wp_head', 'bcloud_insert_request_time', 10 );

function bcloud_insert_request_time(){
    date_default_timezone_set( get_option('timezone_string')); 
    //for this to work, we need to set timezone as a city
    // If you choose a city as a timezone it will save to the "timezone_string" option but if you choose a Manual Offset (UTC+1 for instance) it is saved to the "gmt_offset" 
    // option (as "1") and timezone_string remains empty

    echo '<meta name="bcloud_request_time" content="' . date('H:i:s d-m-Y') . '" timezone="' . get_option('timezone_string') . '">';
}

This above snippet will output the result like this:

meta tag in the header of page showing page creation time - to be used for cache testing
Meta tag in the header, showing page creation time – to be used for cache testing

More To Explore

Let's Talk

Ready To Make Your WordPress Project A Success?
Fill out the form to share your contact details.
We will respond within 24 hours!

Hi There!
close