I am trying to enqueue a script for a custom plugin and I want to include a version number for cache busting. However, the ?ver= query is not being appended to the script name in the html and therefore cache busting is failing.
The php code:
wp_enqueue_script('bundle', plugins_url('dist/client.js', __FILE__), array(), '20190520430', true);
And the html tag that is being produced (with identifiable info removed):
<script type="text/javascript" src="/wp-content/plugins/plugin-name/dist/client.js"></script>
Any help or explanation is appreciated, thanks!
I am trying to enqueue a script for a custom plugin and I want to include a version number for cache busting. However, the ?ver= query is not being appended to the script name in the html and therefore cache busting is failing.
The php code:
wp_enqueue_script('bundle', plugins_url('dist/client.js', __FILE__), array(), '20190520430', true);
And the html tag that is being produced (with identifiable info removed):
<script type="text/javascript" src="https://site.url/wp-content/plugins/plugin-name/dist/client.js"></script>
Any help or explanation is appreciated, thanks!
Share Improve this question edited May 20, 2019 at 21:02 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked May 20, 2019 at 20:47 RyanRyan 31 silver badge2 bronze badges 3 |1 Answer
Reset to default 0You don't strictly need wp_register_script()
but it is considered a best practice to use it as it allows other developers to programmatically work with the order and list of scripts being included.
Regarding your issue, your code looks correct. Be sure to call it inside the wp_enqueue_scripts
action (but I don't think that's influencing the behavior you are seeing).
If I had to guess, and I kind of do, I would guess that there is some other plugin or system configuration that is stripping the query string as part of a caching mechanism. The thought is that query string parameters, if they are present, can keep some proxy servers, and possibly browsers, from caching the resource. There are even some plugins such as Ratify and possibly W3 Total Cache that will strip query string parameters from resources for you, intentionally. The way to test if another plugin is interfering is by disabling them and seeing if anything changes.
If you are using a caching server or CDN, they may also be stripping the query string (but seems unlikely). Cloudflare offers a maintenance mode that allows you to briefly disable the CDN while you test things.
HTH and please let me know how you get on.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745478763a4629466.html
wp_register_script()
for that script? – Derek Held Commented May 20, 2019 at 20:53wp_register_script('bundle', plugins_url('dist/client.js', __FILE__), array(), '20190520430', true); wp_enqueue_script('bundle');
and it results in the same thing? – Ryan Commented May 20, 2019 at 21:52wp_enqueue_script
, don't worry about not callingwp_register_script
. Ryan, is this happening with other scripts too? Or just this one? – Tom J Nowell ♦ Commented May 21, 2019 at 0:21