Create a custom function to remove the WordPress version number from the source code

Emily T. Dec 19, 2024 Security
How can I hide the version number of my WordPress site so it’s not visible to visitors?
What is the best way to create a custom function in WordPress to remove the version number from the HTML source code for enhanced security?
Andy answered Dec 19, 2024

Removing WordPress Version Number from Source Code

Custom Code Solution

The WordPress version number appears in two places in your source code:

  1. As a meta tag generator
  2. In CSS and JavaScript file URLs

Here's a complete solution to remove both:

Add this code to your theme's functions.php file:

This function removes the WordPress version from the head section and RSS feeds:

function remove_wp_version() {
    return '';
}
add_filter('the_generator', 'remove_wp_version');

This function removes version numbers from CSS and JavaScript files:

function remove_version_from_scripts($src) {
    if (strpos($src, 'ver=')) {
        $src = remove_query_arg('ver', $src);
    }
    return $src;
}
add_filter('style_loader_src', 'remove_version_from_scripts', 9999);
add_filter('script_loader_src', 'remove_version_from_scripts', 9999);

Combined Solution

Here's both functions combined into a single, ready-to-use code block:

function hide_wp_version_numbers() {
    // Remove version from head
    add_filter('the_generator', function() { return ''; });
    
    // Remove version from scripts and styles
    function remove_version_from_scripts($src) {
        if (strpos($src, 'ver=')) {
            $src = remove_query_arg('ver', $src);
        }
        return $src;
    }
    add_filter('style_loader_src', 'remove_version_from_scripts', 9999);
    add_filter('script_loader_src', 'remove_version_from_scripts', 9999);
}
add_action('init', 'hide_wp_version_numbers');

Where to Add the Code

  1. Add the code to your theme's functions.php file
  2. If using a child theme (recommended), add it to the child theme's functions.php
  3. Alternatively, create a site-specific plugin for this functionality

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Hide My WP Ghost - Security plugin with version number hiding
  2. Security Headers - Includes version number removal
  3. Perfmatters - Performance plugin with security features including version number removal

Important Notes

  • Removing version numbers is a basic security measure but shouldn't be your only security implementation
  • Keep WordPress, themes, and plugins updated regularly
  • Test your site after implementing these changes to ensure nothing breaks
  • Consider implementing additional security measures alongside version number removal