0

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!

3
  • What's your wp_register_script() for that script?
    – Derek Held
    Commented May 20, 2019 at 20:53
  • Where am I supposed to put it? I can have wp_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:52
  • You can enqueue and register at the same time with wp_enqueue_script, don't worry about not calling wp_register_script. Ryan, is this happening with other scripts too? Or just this one?
    – Tom J Nowell
    Commented May 21, 2019 at 0:21

1 Answer 1

0

You 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.

1
  • 1
    It turned out to be a setting in the KLEO child theme that turned off query strings. Thanks for helping!
    – Ryan
    Commented May 21, 2019 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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