Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Expanding on the question in the title:

I would like to find a way (via a plugin) to use timestamps for the JS and CSS file version query strings that are output with wp_register_style and wp_register_script.

I know this can be easily modified in the calls themselves, and I do it this way currently:

$style_mtime = filemtime(dirname(__FILE__) . '/css/style.css');
wp_register_style( 'fstop-stylesheet', get_stylesheet_directory_uri() . '/library/css/style.css', array(), $style_mtime , 'all');
wp_enqueue_style( 'fstop-stylesheet' );

I would love it if I could pass the timestamp portion off into a plugin. I recently found this plugin that does a good job of moving the query string into the filename, which should result in better caching for proxies: https://gist.github.com/ocean90/1966227

And it does work well. I would like to add the timestamp bit in to automate the whole process.


share|improve this question
possible duplicate of Remove ?ver= from wp_register_script –  Chris_O Feb 6 '13 at 3:01
The linked question is not a duplicate, not even close. –  toscho Feb 6 '13 at 6:37
Yeah I am definitely not asking how to remove query strings, rather how to automate the process of adding the file's timestamp as the query string. In the process of searching I found another angle, which is a plugin that takes the default query string value and moves it to the filename. It works quote well: gist.github.com/ocean90/1966227 Now I need to find a way to make the apache .htaccess rewrite rules needed for filename revving work with WP Engine's nginx setup. –  Gabriel Feb 6 '13 at 18:29
add comment

1 Answer

Old answer (based on misconception that you wanted a cache buster): You can use add_query_arg() which adds/replaces query arguments.

* Plugin Name: wpse_84670
* Version: 0.0.1
* Description: replace script/style version with time as a cache buster

* replace script or stylesheet version with current time
* @param  string $url the source URL
* @return string
function wpse_84670_time_as_version($url) {
    return add_query_arg(array('ver' => time()), $url);

add_filter('script_loader_src', 'wpse_84670_time_as_version');
add_filter('style_loader_src', 'wpse_84670_time_as_version');

New answer: don't do that! It will force a file access for every enqueued script and stylesheet on a page, and depending on what plugins you have activated that could mean an additional dozen or more file accesses for every access to a page/post. Many of them won't even result in the browser requesting those files (if you have expired times configured for scripts and stylesheets -- and you should!)

Instead, just wrap a function around your enqueuing code for your theme, so that you only make a file access for the files your theme enqueues.

Better still, keep a rolling version number in your theme (in my themes, I call it $forceLoad) and use that as the version passed to wp_enqueue_script. No additional file access required.

$forceLoad = '42';
wp_enqueue_style('fstop-stylesheet', get_stylesheet_directory_uri() . '/library/css/style.css', false, $forceLoad, 'all');
share|improve this answer
Wouldn't this use the current time as the version string? That would mean that the file never gets cached, as the version string would constantly change. –  Gabriel Feb 6 '13 at 17:53
Sorry, I thought that was that you were asking for. I'll reread your question after coffee :) –  webaware Feb 6 '13 at 22:27
I am wanting to use the filemtime or the time the file was last saved. This way the file will be cached until it is changed. I am able to do this manually as in the code example in my original question. I would just love to find a way to programmatically add the filemtime as the query string. –  Gabriel Feb 8 '13 at 16:07
Maybe someone else will help you with that then. I won't because it's a silly thing to do with all enqueued scripts/stylesheets, imposing additional file access (performance hits), when you only need it to happen on your own scripts/stylesheets. As such, I reckon you should wrap it in a function and call it manually. Or do like I do, and set a $forceLoad variable (when in development, I set it to current time, e.g. $forceLoad = time();, but otherwise I set it to an incrementing integer) –  webaware Feb 9 '13 at 7:21
How would this impose "additional file access (performance hits)"? Are you saying the process of using filemtime for the query string value for all enqueued files is resource intensive? Or are you saying this would somehow affect caching of files that don't change often, like scripts styles other than my own? There wouldn't be any cache performance issues, because scripts styles other than mine that rarely change would keep the same filemtime value and hence continue to be cached until they were updated. –  Gabriel Feb 12 '13 at 19:13
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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