ThisCalculator

Everyday

Remove Query Strings from Static Resources in WordPress

Updated · Standard formula, shown below

Remove Query Strings from Static Resources in WordPress

This guide explains how to remove query strings (like ?ver=5.8.3) from static resource URLs (CSS and JavaScript files) in WordPress. This can improve page speed and caching efficiency by allowing browsers and CDNs to cache these files more effectively. You’ll need FTP access or a file manager in your hosting control panel, and a code editor.

What You’ll Need

  • Access to your WordPress site’s files (via FTP, cPanel File Manager, or a plugin like WP File Manager).
  • A code editor (like Notepad++ or VS Code) to edit your theme’s functions.php file.
  • A backup of your site (files and database) before making any changes.

Step-by-Step

  1. Backup your site. Use a backup plugin or your hosting provider’s backup tool to create a full backup. This is essential so you can restore if anything goes wrong.
  2. Access your theme’s functions.php file. Go to Appearance → Theme File Editor in your WordPress admin, or connect via FTP and navigate to /wp-content/themes/your-theme-name/functions.php. If you use a child theme (recommended), edit the child theme’s functions.php.
  3. Add the removal code. At the end of the functions.php file, add the following code:
    // Remove query strings from static resources
    function remove_query_strings() {
        if(!is_admin()) {
            add_filter('script_loader_src', 'remove_query_strings_split', 15);
            add_filter('style_loader_src', 'remove_query_strings_split', 15);
        }
    }
    function remove_query_strings_split($src){
        $output = preg_split("/(&ver|\?ver)/", $src);
        return $output[0];
    }
    add_action('init', 'remove_query_strings');
        

    This code removes the ?ver=... part from all CSS and JS file URLs on the front end of your site.

  4. Save the file. Click the “Update File” button if using the theme editor, or upload the modified file via FTP.
  5. Clear all caches. If you use a caching plugin (like WP Super Cache, W3 Total Cache, or LiteSpeed Cache), clear its cache. Also clear your browser cache and any CDN cache (e.g., Cloudflare).
  6. Verify the change. View your site’s source code (right-click → View Page Source) and search for ?ver=. You should see URLs without query strings, like /wp-content/themes/your-theme/style.css instead of style.css?ver=1.0.0.

Common Mistakes

  • Editing the wrong file. Make sure you’re editing the active theme’s functions.php. If you use a child theme, edit the child theme’s file, not the parent’s.
  • Not using a child theme. If you edit the parent theme directly, your changes will be lost when the theme updates. Always use a child theme for custom code.
  • Forgetting to clear caches. If you don’t clear all caches, you might still see query strings in the browser, leading you to think the code didn’t work.
  • Copying code with syntax errors. A missing semicolon or bracket can break your site. Always double-check the code.

Tips

  • Use a child theme to keep your changes safe from theme updates.
  • Test the change on a staging site first if possible, to avoid affecting your live site.
  • If you’re not comfortable editing code, use a plugin like “Remove Query Strings From Static Resources” (but be aware that plugins add overhead).
  • After removing query strings, ensure your server or CDN is configured to send long cache expiration headers for static files (e.g., Cache-Control: max-age=31536000) to get the full benefit.

Troubleshooting

If the query strings are still present after following the steps:

  • Check if you’re viewing a cached version. Clear your browser cache, and use an incognito window to view the source.
  • Verify the code is active. Make sure the code is in the correct functions.php and that the file is saved. You can add a test line like error_log('test'); to see if the file is being loaded.
  • Check for plugin conflicts. Some plugins or themes add query strings themselves. Temporarily disable all plugins (except caching) and switch to a default theme to see if the issue persists. If it does, re-enable them one by one to find the culprit.
  • Server-side caching. If you have server-level caching (like Varnish or Nginx), you may need to clear that as well.
  • If your site goes blank (white screen), you likely have a syntax error. Access the file via FTP and revert to the backup, or remove the added code.

Frequently Asked Questions

Will removing query strings break my site?

No, it’s generally safe. Query strings are only used for cache busting; removing them means browsers might serve a cached version of a file that has changed, but in practice, file names change when files are updated, so it’s rarely an issue.

Do I need to remove query strings from all static resources?

It’s best to remove them from CSS and JS files. Images and fonts typically don’t have query strings, but if they do, you can extend the code to handle them.

Will this improve my PageSpeed score?

It can help, especially if you have many static resources with query strings, as it allows browsers to cache them more aggressively. However, the impact is often small compared to other optimizations like image compression and minification.

Can I do this with a plugin instead of code?

Yes, there are plugins like “Remove Query Strings From Static Resources” that do this without coding. However, plugins add overhead, and the code solution is lightweight and permanent.

Why does WordPress add query strings in the first place?

WordPress adds ?ver= to force browsers to fetch a new version of a file when the version number changes (e.g., after a theme or plugin update). This is a cache-busting mechanism.

Will this affect my site’s performance on mobile?

Yes, it can improve mobile performance by reducing the number of requests that are not cached, which can reduce load times on slower connections.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.