Saturday 5 March 2016

WordPress : Remove Version Query Strings From JavaScript JS and CSS Stylesheet Files

In efforts to increase the performance of a WordPress website, tools like Google Page Speed and Yahoo YSlow may direct you to remove the query string from JavaScript and CSS files. Most scripts and stylesheets called by WordPress include a query string identifying the version. This can cause issues with caching and such, which will result in less than optimal load times.

For instance, jQuery.js may show up like:

/wp-includes/js/jquery/jquery.js?ver=1.4.2


there is a very simple function that can be added to remove all query strings from script and css files.
In your theme’s function file (Appearance > Editor > Functions.php), add the following code before the closing PHP tag (%>) at the bottom of the file:


function _remove_script_version( $src ){
$parts = explode( '?', $src );
return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );


Hope you find this helpful!

No comments:

Post a Comment