Skip to content

Instantly share code, notes, and snippets.

@lilumi
Last active February 27, 2017 18:04
Show Gist options
  • Save lilumi/70c53abe12ada4e6868bffad3b19764e to your computer and use it in GitHub Desktop.
Save lilumi/70c53abe12ada4e6868bffad3b19764e to your computer and use it in GitHub Desktop.
Add ver filemtime to styles
<?php
add_filter( 'script_loader_src', 'remove_src_version' );
add_filter( 'style_loader_src', 'remove_src_version' );
function remove_src_version ( $src ) {
global $wp_version;
$version_str = '?ver='.$wp_version;
$version_str_offset = strlen( $src ) - strlen( $version_str );
$version_str_alt = '&ver='.$wp_version;
$version_str_offset_alt = strlen( $src ) - strlen( $version_str_alt );
if( substr( $src, $version_str_offset ) == $version_str )
$without_ver = substr( $src, 0, $version_str_offset );
elseif (substr( $src, $version_str_offset_alt ) == $version_str_alt) {
$without_ver = substr( $src, 0, $version_str_offset_alt );
}
else $without_ver = $src; //return original url if no version
$file_path = str_replace(get_stylesheet_directory_uri(), get_stylesheet_directory(), $without_ver);
$file_path_alt = str_replace(get_template_directory_uri(), get_template_directory(), $without_ver);
if (file_exists($file_path)) {
return $without_ver.'?ver='.filemtime($file_path);
} elseif (file_exists($file_path_alt)) {
return $without_ver.'?ver='.filemtime($file_path_alt);
}
else return $without_ver;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment