Remove Query Strings from Static Resources

remove query strings

Without using any WordPress plugin you can easily remove query strings from your assets with a few lines of code. Simply add the following to your WordPress theme functions.php file.

/**
 * Remove query string from static resources.
 */
function remove_query_strings() {
   // Check if admin backend.
   if( ! is_admin() ) {
       add_filter( 'script_loader_src', 'remove_query_strings_version', 20 ); // Remove query string of JS static resources
       add_filter( 'style_loader_src', 'remove_query_strings_version', 20 ); // Remove query string of CSS static resources
   }
}

/**
 * Hooks for splitting url to query and return only the resources static url.
 */
function remove_query_strings_version( $src ){
   $arr = preg_split( "/(&ver|\?ver)/", $src );
   return $arr[0];
}
add_action( 'init', 'remove_query_strings' );

Stay in the loop with our web development newsletter - no spam, just juicy updates! Join us now. Join our web development newsletter to stay updated on the latest industry news, trends, and tips. No spam, just juicy updates delivered straight to your inbox. Don't miss out - sign up now!


We’ve tried our best to explain everything thoroughly, even though there’s so much information out there. If you found our writing helpful, we’d really appreciate it if you could buy us a coffee as a token of support.

Also, if you’re interested in learning more about WordPress, Javascript, HTML, CSS, and programming in general, you can subscribe to our MailChimp for some extra insights.

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.