Skip to content

Instantly share code, notes, and snippets.

@mohammad-ahid
Created January 16, 2017 14:45
Show Gist options
  • Save mohammad-ahid/507535259827c2cb608434bd4e6a81b1 to your computer and use it in GitHub Desktop.
Save mohammad-ahid/507535259827c2cb608434bd4e6a81b1 to your computer and use it in GitHub Desktop.
Remove query strings from enqueued resources in WordPress
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment