Skip to content

Instantly share code, notes, and snippets.

@gpbeer
Created April 14, 2017 14:49
Show Gist options
  • Save gpbeer/4764ee3003bd84312caef60e10e1e041 to your computer and use it in GitHub Desktop.
Save gpbeer/4764ee3003bd84312caef60e10e1e041 to your computer and use it in GitHub Desktop.
Wordpress: Get javaScript and Stylesheet files data version for cache busting purposes
/**************************************************
* 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' );
/*
Page: Page One StyleSheet
Version: 1.1.19
*/
div {
}
// Page: Page One JavaScript
// Version: 1.1.19
$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