WordPress How To Remove Query Strings From CSS & JS


About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







WordPress How To Remove Query Strings From CSS & JS

Everybody wants a faster website and who can blame you?  You spend hours drooling over Google’s Page Speed Insights test but you have no idea how to improve on the number.  Here’s a quick code snippet on how to remove the version query strings from WordPress JS and CSS files using only a few lines of code.

Simply add the following to your themes functions.php file and save the file.  This will automatically work out of the box with any asset that is being loaded using wp_enqueue_script or wp_enqueue_style which should be any file being loaded by your theme or plugins.

 

function wpc_remove_script_version( $src ){
    return remove_query_arg( 'ver', $src );
}

add_filter( 'script_loader_src', 'wpc_remove_script_version' );
add_filter( 'style_loader_src', 'wpc_remove_script_version' ); 

 

It should be noted that this can effect caching and proper renders of updated files if you use a caching plugin or a CDN service such as Cloudflare. When WordPress or your plugins are updated, a new version number is appended to the end of the resource which tells the caching service that the file has changed. Removing these version numbers will cause the caching service to not be aware if/when the file is changed. Therefore, you need to be proactive after a WordPress or plugin update to manually clear the cached resources of that plugin/theme to ensure that the newest version will be called.

This change can effectively improve your Google Page Speed Insights test as well as your search engine optimization.


Comments:

  1. jim says:

    Could you explain why this makes things faster? If it is the same file, same size, what does having a version number do to speed things up? As you mention, this will cause problems for caching management.

    1. Cabe Nolan says:

      Hi Jim, Resources with a “?” or “&” in the URL are not cached by some proxy caching servers. Therefore, removing these query strings from the URLs can have a positive impact in those scenarios and cause the resources to be cached. You will notice a higher score using tools like http://tools.pingdom.com when query strings are not present in these resources.

Share Your Thoughts

Leave a Reply

Your email address will not be published.


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!