Remove query strings from WordPress assets to allow caching.

When you’re trying to squeeze every available bit of performance from your website, you’ve no doubt looked into caching.

When caching, most assets with a query string are not cached, so this will hamper your efforts and decrease performance.

What is a query string? A query string is the ?something= parameter in the URL.

WordPress automatically adds version query strings to your CSS & JS assets when they are loaded.

If you look at your website source, you’ll see something like <link rel="stylesheet" src="https://domain.com/wp-content/themes/themename/style.css?ver=1.2.3" type="text/css" media="all" />.

Notice the ?ver=123, that’s the query string blocking your caching.

How do we do it?

Removing is relatively easy, let’s take a look. Simply add the following function to your functions.php file.


<?php
if ( ! function_exists( 'uncoverwp_remove_query_strings' ) ) :
    /**
     * Remove query strings from
     * assets for caching
     */
    function uncoverwp_remove_query_strings( $src )
    {
        return remove_query_arg( 'ver', $src );
    }
    add_filter( 'script_loader_src', 'uncoverwp_remove_query_strings' );
    add_filter( 'style_loader_src', 'uncoverwp_remove_query_strings' );
endif;

This simply adds our function to remove the query argument on the script and style loader tags, thus removing the query string from the assets URLs.

Conclusion.

A very easy way to boost your sites performance and allow full caching of assets.

UncoverWP

Want us to email you occasionally with our latest courses, news & updates?

We use cookies to ensure that we give you the best possible experience on our website. If you continue to use this site we will assume that you are happy with it. OK Privacy Policy