Created
May 12, 2014 06:41
-
-
Save ebinnion/c04265e34151a5e9a14e to your computer and use it in GitHub Desktop.
A method to enqueue and auto version CSS and JavaScript in WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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