Last active
July 7, 2025 19:52
-
-
Save herbie4/b8c754cfd49c98fef44c243bc3995906 to your computer and use it in GitHub Desktop.
WordPress: Obfuscate all version from css and js sources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Obfuscate all version from css and js sources | |
* Plugin URI: https://haha.nl/wordpress-plug-in-op-maat/ | |
* Description: Encode version numbers from the source files so to keep files version dependant but obscure the WP version but still maintain version control on these files. | |
* Version: 1.0.1 | |
* Author: herbert hoekstra - haha! | |
* Author URI: https://haha.nl | |
* Documentation URI: https://haha.nl/wordpress-plug-in-op-maat/ | |
*/ | |
// base 64 encode url safe version numbers | |
// obfuscate wp version for bots | |
// ========================================= | |
// encode and make url safe | |
function base64_encode_url($string) { | |
return str_replace(['+','/','='], ['-','_',''], base64_encode($string)); | |
} | |
// Remove version only if it comes from WP version | |
function hhdev_remove_ver_css_js( $src_file ) { | |
$version = get_bloginfo( 'version' ); | |
$version_hidden = ''; | |
if ( strpos( $src_file, 'ver=' ) ) { | |
$version_hidden = base64_encode_url($version); | |
// print_r($version_hidden); | |
$src_file_clean = remove_query_arg( 'ver', $src_file ); | |
$src_file_new = add_query_arg('ver', $version_hidden , $src_file_clean); | |
return $src_file_new; | |
} | |
return $src_file; | |
} | |
add_filter( 'style_loader_src', 'hhdev_remove_ver_css_js', 9999 ); | |
add_filter( 'script_loader_src', 'hhdev_remove_ver_css_js', 9999 ); | |
// remove more versions | |
// uncomment if needed | |
// ======================== | |
//function hhdev_remove_version() { return ''; } | |
//add_filter('the_generator', 'hhdev_remove_version'); | |
//remove_action('wp_head', 'wp_generator'); | |
/* | |
change log | |
1.0.1 fixed: 'ver' was added to urls without ver defenition | |
1.0.0 initial set up | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment