Created
April 14, 2017 14:49
-
-
Save gpbeer/4764ee3003bd84312caef60e10e1e041 to your computer and use it in GitHub Desktop.
Wordpress: Get javaScript and Stylesheet files data version for cache busting purposes
This file contains hidden or 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
/************************************************** | |
* Get version from source file | |
***************************************************/ | |
if (!function_exists('get_file_data_version')) : | |
/** | |
* @param string $file_path must contain file url | |
* @return string of version number key. | |
*/ | |
function get_file_data_version($file_path) | |
{ | |
$get_file_data_version = !empty($file_path) ? get_file_data($file_path, array ( 'Version' )) : null; | |
if (WP_DEBUG === true) // If debug is active in development | |
return '1.1.' . time(); | |
return $get_file_data_version[0]; | |
} | |
endif; | |
/************************************************** | |
* Enqueue scripts and styles. | |
***************************************************/ | |
function enqueue_theme_scripts() { | |
// CSS stylesheet: | |
wp_enqueue_style( 'theme-stylesheet-one', get_template_directory_uri() . '/assets/css/stylesheet-one.css', false, get_file_data_version(get_template_directory_uri() . '/assets/css/stylesheet-one.css') ); | |
// JavaScript: | |
wp_enqueue_script( 'theme-script-one', get_template_directory_uri() . '/assets/js/stylesheet-one.js', array( 'jquery' ), get_file_data_version(get_template_directory_uri() . '/assets/js/stylesheet-one.js'), true ); | |
} | |
add_action( 'wp_enqueue_scripts', 'enqueue_theme_scripts' ); |
This file contains hidden or 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
/* | |
Page: Page One StyleSheet | |
Version: 1.1.19 | |
*/ | |
div { | |
} |
This file contains hidden or 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
// Page: Page One JavaScript | |
// Version: 1.1.19 |
This file contains hidden or 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
$version: "1.1."+random(100) ; | |
/* | |
Page: Page One StyleSheet | |
Version: #{$version} | |
*/ | |
div { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment