Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jan 6, 2015

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
You can’t perform that action at this time.