Skip to content

Instantly share code, notes, and snippets.

@ebinnion
Created May 12, 2014 06:41
Show Gist options
  • Save ebinnion/c04265e34151a5e9a14e to your computer and use it in GitHub Desktop.
Save ebinnion/c04265e34151a5e9a14e to your computer and use it in GitHub Desktop.
A method to enqueue and auto version CSS and JavaScript in WordPress.
<?php
/**
* Auto-versioning CSS and JavaScript in WordPress
* @author Eric Binnion
* http://manofhustle.com
*/
add_action("wp_enqueue_scripts", "auto_version_scripts", 20);
function auto_version_scripts() {
// Get last modified timestamp of CSS file in /css/style.css
$ctime = template_directory() . '/css/style.css' );
// Get last modified timestamp of JS file in /js/main.js
$jtime = template_directory() . '/js/main.js' );
wp_enqueue_style(
'custom_style', // handle for style.css
get_template_directory_uri() .'/css/style.css' ,
array(), // dependencies
$ctime, // version number
true // load in footer
);
wp_enqueue_script(
'custom_js', // handle for main.js
get_template_directory_uri() .'/js/main.js' ,
array(), // dependencies
$time, // version number
true // load in footer
);
}
@carlotrimarchi
Copy link

Hi,
at rows 11 and 14 there seems to be a syntax error. At the end there is a closed parenthesis. Also the function "template_directory" doesn't exist.

I think the correct version would be:
$ctime = filemtime(get_template_directory() . '/css/style.css' );

Anyway thanks, this was useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment