Easily Remove Query Strings from Static Resources In WordPress Websites

elegant themes affiliate

Want to score well in Google page speed test. Then you have to remove query string from all CSS and javascript files in your WordPress website.

There are many different factors which affect your total page load time and Google page speed test consider all of those factors. According to Gtmetrix, removing query strings from static resources like stylesheet and javascript has the higher priority which is considered during the speed test.

Note: If you don’t like reading and want one simple solution to remove query string then download my plugin, install it and forget it. It will do all complex task in the background and you don’t even need to configure it.

Download

What Is Query String?

The query string is a method by which different plugins pass the content values or carry the version detail. They are used in both javascript and stylesheets. Actually, it’s one of the methods implemented by developers to instantly render new updates.

Here’s the explanation. Let’s say you are a developer and you have created a plugin which has few CSS and js files. Now if anyone who is using your plugin and also using a CDN service then these static files ( both CSS and js files ) will be cached for weeks, months or for even one year.

Now if you push up any update to your plugin it will not be visible until the cached file expires. So developers use the query string as it is not cached by most of the browsers and CDN services. Overall it might be helpful for web developers and designers but is a bad practice and have a great impact on your website’s loading speed.

How To Remove Query String From Static Resources

remove query string from wordpress

I have two different options for you. You can either chose to edit your theme manually and add the custom code to your function.php file. Or you can go with the plugin which is, as usual, easy to install.

#1 Using Custom Code

I have already developed a plugin do this task, but if you still want to do it manually then follow the steps properly. You have to just place it in your current theme’s function.php file. You can either choose to edit your theme via WordPress dashboard or you can even use FTP or online file manager.

Simply navigate to your WordPress dashboard then to appearance. Then from on hover menu select the editor. Now on the right-hand side search for function.php and place the code at the bottom.

// Remove query string from static files
function remove_cssjs_ver( $src ) {
 if( strpos( $src, '?ver=' ) )
 $src = remove_query_arg( 'ver', $src );
 return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

With the help of this code ?ver string will be removed. It can be altered as per need. But before making any changes, I will request you to create a backup of theme’s function file.

#2 Using A Plugin

I think it is the easiest way to manage all types of “?” queries. You don’t need to edit any file, not you have to create any backup. Just install remove query strings from static resources plugin and you are done.

If you are still facing any problem then you can contact me directly from the comment section.

26 thoughts on “Easily Remove Query Strings from Static Resources In WordPress Websites

    • Problem is that, you don’t know who to use it.

      You can download my wordpress plugin if you don’t have any idea on how to use the code snippet.

  1. Your plugin has broken functionality of my website and now I can’t revert it even if I disable and delete your plugin!!

    Thank you!

    • I have been using this plugin for more than 2 years. It has also been updated so as to be compatible with latest wordpress updates. It is being used by thousands and none complained me about it.

  2. Hi Vivek
    I tried the plugin, No change
    I tried the code (if I did it right), No change.
    I think I have used 2-3 days to google around and solve it (I’m persistent… 😉 )
    I hope You have an idea.
    This is my Pingdom test: http://tools.pingdom.com/fpt/#!/dLNcDz/https://energig.com/da

    And my functions.php from my Child theme:

    <?php
    //
    // Recommended way to include parent theme styles.
    // (Please see http://codex.wordpress.org/Child_Themes#How_to_Create_a_Child_Theme)
    //
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
    get_stylesheet_directory_uri() . '/style.css',
    array('parent-style')
    );
    }
    //
    // Your code goes below
    //

    I tried pasting your code in the bottom as described but… No change 🙁

    I have tried finding other solutions, but I'm not techie enough.

    I really hope that you have inputs 🙂

    //Flofre

      • Thanks Vivek
        Yes the 97 upon 100 is not bad. Now I have to find out why it takes 4 seconds to load. Same page was 1 second last week and I don’t think anything big has changed, so I have set some parameter somewhere that makes the page wait almost 3 second in the pingdom waterfall first 2 lines.
        But thats not your problem 🙂
        Thanks 🙂

  3. This snippet works just for css and javascript files. And does its job, thanks!
    Now i need a way to do the same for an embedded video file.
    Is there any way to get rid of the query string for a video file in the content section? Preferably without the overhead of filtering the whole content… 🙂

  4. Hi,

    I’ve installed the plugin but it won’t work. I dit some new tests on pingdom.com but no better results.

    Can you help me out?

    Greetz Mick

    • 1. You have to install this plugin :: https://wordpress.org/plugins/remove-query-strings/
      If you have already installed it, then deactivate it and then re activate it.

      2. If you are using any cache plugin like wp rocket, wp super cache or w3 total cache, then please clear the cache or purge the cache.

      3. If you are using cloudflare then please purge all your cache.

      Then visit your website and hard refresh your website by ctrl+f5 ( on chrome ) and right click and choose view page source.

      Search the page ( ctrl+f ) for any css file and most probably the ? ( query string ) must have been removed.

  5. I’m thinking of trying this plugin. Ironically, pingdom notes 4 query strings (and it’s all from my email application MAILERLITE. ) Do not understand any of it)
    Another STRANGE thing is that on Google Page Speed,,, it’s not even mentioned! (97 desktop 100 mobile!!) what are your thoughts?
    Howard

    • It seems that its cache problem. I will suggest flushing all your cache, including CDN. And then try the Pingdom test. If you still see any query string warning please share the test url with me.

  6. Hi Vivek,
    I have tried both methods and still when I do the Pingdom test and GT test get reports that queries still exist. I am no specialist in this area so really appreciate any help you can provide.
    If I have installed the app correctly and put the code in correctly is there a reason that I would still be having these issues?

    Below are the items with queries
    https://i0.wp.com/www.buildersolutions.com.au/w … 7/02/About-Our-Builders.jpg?fit=1136%2C810&ssl=1

    • Your problem cannot be solved with this plugin. The problem you are facing is because of wordpress.com CDN service. I am 100% confident that you are using Jetpack’s photon service.

      The query string which you are seeing is actually to help you make your website faster by serving the best optimized image considering the current screen resolution.

  7. hello, I have added the string manually but looks like it doesn´t Work when I am running the test on pingdom.com.

Leave a Comment