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
- Edit the wp-config.php for each of your instances.
- Add the following (if it doesn’t already exist):
1define( 'WP_ENV', 'development');
(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.