How to remove the version number from scripts enqueued in WordPress

To remove the version number from scripts enqueued with wp_enqueue_script(), you can use the wp_enqueue_script function without specifying the version parameter or by passing null as the version number.

Here’s an example:

function my_theme_enqueue_scripts() {
// Register and enqueue your script without a version number
wp_enqueue_script('my-script-handle', get_template_directory_uri() . '/js/my-script.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_scripts');

In the above code snippet, null is passed as the version number in the wp_enqueue_script() function. This will prevent WordPress from appending the version number as a query string to the script URL.

However, if you want to remove version numbers globally from all enqueued scripts and styles, you can use the following code snippet:

function remove_script_version($src) {
return remove_query_arg('ver', $src);
}
add_filter('script_loader_src', 'remove_script_version', 15, 1);
add_filter('style_loader_src', 'remove_script_version', 15, 1);

Add this code snippet to your theme’s functions.php file. This will remove the version numbers from all enqueued scripts and styles on your WordPress site.

Note: Removing version numbers can have some drawbacks, such as making it harder to clear browser caches when you update a script or style file. To address this issue, consider using a cache-busting technique, such as appending the last modified timestamp of the file as the version number.

Leave a Reply

Your email address will not be published. Required fields are marked *