Snippet: Disable WordPress Enqueued Script Caching

This code snippet will show you how you can disable caching of enqueued scripts in WordPress in your development/staging environment(s) but not in your production environment.

Add Environment Variable

  1. Edit the wp-config.php for each of your instances.
  2. Add the following (if it doesn’t already exist):

(replace ‘development’ with whatever label you want for each environment, such as ‘staging’, ‘production’, etc.)

Add/Modify Code to Enqueue Your Custom Scripts

We’re now going to tell our enqueued scripts to either:

  • If WP_ENV is ‘production’, when append the theme version to the script.
  • If WP_ENV is anything else (like ‘development’ or ‘staging’), append a timestamp to the script link.

In this way, whenever the above files are modified, a new timestamp will be appended and the web browser will load the new content. This is more convenient while developing because you do not have to manually refresh the scripts as you modify them.

Of course, you may modify the logic as you desire, break away the theme version login into a separate string for better readability, etc.

Leave a Reply

Your email address will not be published. * Denotes required field.
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Tip: Do you want an image (avatar) next to your posts? Sign up for a free Gravatar account. It only takes 5 minutes - simply click the "Create Your Own Gravatar" button, provide your e-mail address and upload a photo to associate with it.