0

I'm making a WordPress plugin which loads an external JS file for some Google Maps related operations. But the problem is that the changes I make to this external JS file are not applied immediately and shown on the Google Chrome browser, but instead I have to delete the "Images and other data" cache of Chrome in order for the changes I make to the JS file to be applied.

For example I have an alert("DynamicMap.js file was loaded succesfully.); in the first line of the file but the changes I make to the text only apply after I clear the cache. Does anyone know why does this happen and if I can prevent it? Thanks in advance. (Note that I'm testing plugin online on my personal website, not on the localhost.)

1

Use function filemtime($path), where $path is a path to your script, in wp_enqueue_script call, as fourth parameter (version). Every time you modify the script, the cache will be bypassed.

Update

Calls to wp_register_script and wp_enqueue_script:

wp_register_script('DynamicMapScript', plugins_url('DynamicMap.js', __FILE__), array('jquery'), filemtime(dirname(__FILE__) . '/DynamicMap.js'));
wp_enqueue_script('DynamicMapScript');
  • Thanks for the suggestion. Unfortunately it didn't work. I called the function filemtime($path) both in my wp_register_script call and wp_enqueue_script call like this: wp_register_script( 'DynamicMapScript', plugins_url('DynamicMap.js', __FILE__), array('jquery'), filemtime( plugins_url('DynamicMap.js', __FILE__) ) ); and wp_enqueue_script('DynamicMapScript', '', array(), filemtime( plugins_url('DynamicMap.js', __FILE__) ) );. The cache is still not bypassed. – Stavros Skamagkis Apr 21 '18 at 2:28
  • Where, within your plugin folder, DynamicMap.js script is located? – Frank P. Walentynowicz Apr 21 '18 at 3:47
  • It's located in the root folder of my plugin. No sub-folders. – Stavros Skamagkis Apr 21 '18 at 3:53
  • filemtime function expects a path to the file, not URL Replace filemtime(plugins_url('DynamicMap.js', __FILE__)) with filemtime(dirname(__FILE__) . '/DynamicMap.js'), in your code. – Frank P. Walentynowicz Apr 21 '18 at 4:34
  • What do i put in dirname? The full path to my plugin under the base url of my site eg wp-content/plugins/Map_of_resellers or only the subfolder of my plugin under the plugins folder eg Map_of_resellers? – Stavros Skamagkis Apr 21 '18 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.