2

I am creating plugin for first time and facing major issue regarding cache.

I register my js files as

wp_register_script("custom-js-backend", PLUGIN_URL . 'assets/js/custom.js', array(
                'jquery',
                'jquery-blockui'
));
wp_enqueue_script("custom-js-backend");

But unable to reflect changes on admin panel.

Tried

function cache_cleanup()
{
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_footer', 'wp_generator');
}
add_action('init', 'cache_cleanup');  

Also defined in wp-config.php

define('WP_CACHE',false);

But no luck.

Since by default my js file comes with 5.1.0 version so tried to remove the version from it using https://wordpress.org/plugins/disable-version-caching/

Now the version is being removed but still file is not updating.

One solution is adding version number to my js file and changing version after every small change but this is quite not right.

Any help how to disable cache for admin panel while creating plugin.

armin
  • 176
  • 5
  • 16

2 Answers2

0

you can use filemtime(PLUGIN_URL . 'assets/js/custom.js') as a version number - that will make the version number, the unix time of the last time the file was saved -

Meaning that everytime you same the file, you automatically get a new version number higher than the last one, that way it is still cacheable, but will get cleared when there are changes

So it would look like

wp_register_script("custom-js-backend", PLUGIN_URL . 'assets/js/custom.js', array(
            'jquery',
            'jquery-blockui'
), filemtime(PLUGIN_URL . 'assets/js/custom.js'), true);
Stender
  • 2,446
  • 1
  • 14
  • 22
0

Add this to .htaccess to disable browser caching during development:

# Disable browser caching.
# Ensure mod_headers is installed.
# To install on Debian, run `a2enmod headers`, then restart the server.
FileETag None
Header unset ETag
Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
Header set Pragma "no-cache"
Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"

Note: your browser may still cache external content from CDNs, but anything on your server won't be cached.

CrazyTim
  • 6,695
  • 6
  • 34
  • 55