Prevent Version URL Parameter (?ver=X.X.X) on Enqueued Styles & Scripts

Use Case

I’ve been experimenting with Chrome’s Dev Tools Workspace features. It includes the ability to edit a file directly in Dev Tools and have the saved stylesheet refresh itself (or even compile and then refresh!).

However, as documented in the StackOverflow question “Chrome’s “Auto-Reload Generated CSS” not reloading page when SASS recompiles CSS”, URL parameters on the stylesheet URL prevent Chrome from noticing the change.

Desired Outcome

That means that only during development, I wanted to remove the ?ver=X.X.X from the normal stylesheet <link> output by wp_enqueue_style(). In other words, I wanted the default href:

http://localhost/mysite/wp-includes/style.css?ver=4.1.1

to instead be this:

http://localhost/mysite/wp-includes/style.css

3

Default wp_enqueue_[style/script]() behavior

The default value for the $version argument of wp_enqueue_style() is false. However, that default just means that the stylesheets are given the WordPress version instead.

Solution

Thanks to “Remove version from WordPress enqueued CSS and JS”, I learned the undocumented fact that passing in null as a version will remove the version altogether!

Example

wp_enqueue_style( 'wpse-styles', get_template_directory_uri() . '/style.css', array(), null );

Caveat Reminder

It’s worth pointing out, as noted in the question, that this should probably only be done during development (as in the specific usecase). The version parameter helps with caching (and not caching) for site visitors and so probably should be left alone in 99% of cases.

Leave a Comment