Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Ensure Your Scripts and Styles Are Not Cached with Updates

Posted on November 5, 2012 by Pippin in Beginner, Quick Tips, Tutorials 10 Comments
Home» Tutorials » Beginner » Ensure Your Scripts and Styles Are Not Cached with Updates
Tweet
Love It - 3

Have you ever pushed out an update that included modifications to scripts or styles and then later had to tell someone to clear their cache or do a hard refresh in order to see the updates? There is actually a very simple way to avoid this scenario and force the browser to display the updated scripts and styles, even when a caching plugin is in use.

When using wp_enqueue_script() and wp_enqueue_style(), there is a $ver parameter (version) that allows you to pass a file version. This version number is used to determine if a new version should be displayed or not. By default, if you do not include the version number, WordPress automatically gives your script or style the same version number as the current install of WordPress.

Let’s assume that we have loaded a page that has a script set to version 1.0. After loading this script gets cached, so the next time we load the page, we’re actually viewing the cached version. If on our next visit, however, WordPress sees that the script is now version 1.1, it will force the browser to reload the script fresh instead of from cache. This ensures that the new version is always used.

If you have a large plugin or theme, keeping track of script and style version numbers can be difficult, so what I like to do is simply pass the same version number as the plugin or theme. This works really well because anytime an update is released, I have confidence that the new scripts / styles will be used. If no changes were made to any scripts or styles, no harm is done, but if changes were made, they will be displayed.

It’s really great because this even works with caching plugins installed, such as W3 Total Cache or WP Super Cache.

I usually define a constant that holds my plugin version number, like so (example take from Easy Digital Downloads):

1
define( 'EDD_VERSION', '1.3.2' );

In my script loader, I then use it like so:

1
2
wp_enqueue_script( 'edd-ajax', EDD_PLUGIN_URL . 'includes/js/edd-ajax.js', array( 'jquery' ), EDD_VERSION );
wp_enqueue_script( 'edd-validation', EDD_PLUGIN_URL . 'includes/js/form-validation.js', array( 'jquery', 'jquery-validation' ), EDD_VERSION );

Anytime I push out an update, I simply update my EDD_VERSION constant with the new version number and all scripts / styles then use this as their new version.

Tweet Follow @pippinsplugins
wp_enqueue_script, wp_enqueue_style

10 comments on “Ensure Your Scripts and Styles Are Not Cached with Updates”

  1. Gregg says:
    November 5, 2012 at 9:15 am

    Thanks Pippin! This was happening to me the other day and I knew it, just wasn’t sure how to make it simple.

    Reply
  2. Topher says:
    November 5, 2012 at 9:19 am

    Something that annoys me during development is that I’ll make a css change, reload, realize I need to increment the number, reload, etc.

    Is there a way to make it NOT cache while I making changes every couple minutes?

    Reply
    • Pippin says:
      November 5, 2012 at 9:21 am

      I would just disable caching entirely while doing that. Incrementing your version number during dev probably isn’t a great idea.

  3. Zach says:
    November 5, 2012 at 9:26 am

    Good tip! http://wordpress.org/extend/plugins/versionit/ is pretty handy too ;)

    Reply
    • Pippin says:
      November 5, 2012 at 9:38 am

      That’s a great idea! Love it :D

  4. Kaspars says:
    November 5, 2012 at 9:39 am

    Several browsers and proxies do not cache resources that have a query string in the URI (Revving Filenames: don’t use querystring) so it might be better to include the last modified time in the filename itself.

    A few month ago I created the Minit plugin which combines all CSS and JS files into one and stores them in the uploads/minit folder. It works only with scripts and files that are enqueued using the standard enqueue methods. It checks the filemtime of every enqueued file on every request and includes that information in the generated filename. It would be great if more people could test this plugin.

    Reply
    • Pippin says:
      November 5, 2012 at 9:53 am

      The plugin looks good! Also take a look at the one Zach just released: http://wordpress.org/extend/plugins/versionit/

  5. Dawson says:
    November 5, 2012 at 9:51 am

    Thanks for the detailed article. I have been using random version numbers as I dont have much idea about it. From now will implement this and in one go version number will be updated for all scripts and CSS files. Kudos Pippin.

    Reply
  6. John Blackbourn says:
    November 12, 2012 at 2:00 pm

    You can actually eliminate the need to use a plugin version constant by utilising PHP’s filemtime() function. Mark Jaquith explained its use for stylesheets here and it’s easy to convert for use within your plugin using plugin_dir_path().

    Reply
    • Pippin says:
      November 12, 2012 at 7:05 pm

      That works really well too, though it’s really the exact same thing. The difference is that you don’t need the constant, but if you already have a constant defined, then there isn’t any extra.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Using Ajax in Your Plugin and WordPress Admin
  • Loading Scripts Correctly in the WordPress Admin
  • Writing Your First WordPress Plugin Part 4

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

attachments featured hook meta box campaign monitor wp_enqueue_script Rémi Corson shortcodes Tom McFarlin get_user_meta contextual help register_setting add_shortcode image plugin forms do_action short codes mail chimp Related posts login authors attachment post types apply_filters comments recent posts short code bbpress taxonomies custom post type Ajax images gallery Stripe jquery taxonomy widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins