Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmhendricks/4d7c5eb620978dee55536742c2ec1955 to your computer and use it in GitHub Desktop.
Save dmhendricks/4d7c5eb620978dee55536742c2ec1955 to your computer and use it in GitHub Desktop.
Remove script versions from enqueued scripts. #wordpress
<?php
/**
* Remove script versions from frontend static scripts.
* Place this code in your child theme's functions.php OR use the plugin below.
*/
add_filter( 'style_loader_src', 'myprefix_remove_script_versions', 9999 );
add_filter( 'script_loader_src', 'myprefix_remove_script_versions', 9999 );
function myprefix_remove_script_versions( $src ) {
if ( strpos( $src, 'ver=' ) ) $src = remove_query_arg( 'ver', $src );
return $src;
}
?>
<?php
/**
* @wordpress-plugin
* Plugin Name: Remove Script Versions
* Plugin URI: https://gist.github.com/dmhendricks/4d7c5eb620978dee55536742c2ec1955
* Description: Remove the script versions from frontend scripts.
* Version: 1.0.0
* Author: Daniel M. Hendricks
* Author URI: https://www.danhendricks.com
* License: GPL-2.0
* License URI: https://opensource.org/licenses/GPL-2.0
*/
class Remove_Script_Versions {
function __construct() {
if( !is_admin() ) {
add_filter( 'style_loader_src', array( $this, 'remove_script_versions' ), 9999 );
add_filter( 'script_loader_src', array( $this, 'remove_script_versions' ), 9999 );
}
}
function remove_script_versions( $src ) {
if ( strpos( $src, 'ver=' ) ) $src = remove_query_arg( 'ver', $src );
return $src;
}
}
new Remove_Script_Versions();
@dmhendricks
Copy link
Author

I added a simple plugin for those who prefer or don't want to/can't edit their functions.php. Use one or the other.

To use the plugin:

  1. Create a directory in wp-content/plugins named remove-script-versions.
  2. Create a new file. You can name it remove-script-versions.php.
  3. Past the code from wordpress-remove-script-versions-plugin.php into the file. Save.
  4. Activate the plugin.

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