Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Created January 12, 2015 03:02
Show Gist options
  • Select an option

  • Save codearachnid/c58ce37f61cd57c090f9 to your computer and use it in GitHub Desktop.

Select an option

Save codearachnid/c58ce37f61cd57c090f9 to your computer and use it in GitHub Desktop.
WordPress automatically adds query strings to enqueued static resources like stylesheets and scripts. This shall remove them on production sites.
<?php
/**
* WordPress automatically adds query strings to enqueued static resources like stylesheets and scripts.
* This is meant for caching reasons (e.g. if a script version changes,
* it should prevent the client to use a cached copy of the old one).
* However, this feature can be omitted to improve site performance.
*/
function remove_query_strings_questionmark( $src ){
$rqs = explode( '?ver', $src );
return $rqs[0];
}
function remove_query_strings_ampersand( $src ){
$rqs = explode( '&ver', $src );
return $rqs[0];
}
// hook filters
add_filter( 'script_loader_src', 'remove_query_strings_questionmark', 15, 1 );
add_filter( 'style_loader_src', 'remove_query_strings_questionmark', 15, 1 );
add_filter( 'script_loader_src', 'remove_query_strings_ampersand', 15, 1 );
add_filter( 'style_loader_src', 'remove_query_strings_ampersand', 15, 1 );
@codearachnid

Copy link
Copy Markdown
Author

Keep in mind it will fail Google font libraries and possibly other api loaders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment