How to Remove Version Numbers from CSS and JS in WordPress

How to Remove Version Numbers from CSS and JS in WordPress

Here are several methods to remove version numbers from CSS and JS files in WordPress, each with its own considerations:

1. Plugin-Based Solutions:

  • Autoptimize: This popular optimization plugin offers a setting to remove version numbers. Navigate to Settings > Autoptimize > CSS/JS Options and enable the “Remove query strings from static resources” option.
  • W3 Total Cache: Another versatile optimization plugin with a similar feature. Go to Performance > General Settings and enable the “Remove query strings from static resources” option.

2. Manual Removal within Theme Files:

  • Access your theme’s functions.php file.
  • Add code to remove version numbers:
PHP
function remove_query_strings_from_static_resources( $src ) {
    if ( strpos( $src, '?ver=' ) ) {
        $src = remove_query_arg( 'ver', $src );
    }
    return $src;
}
add_filter( 'script_loader_src', 'remove_query_strings_from_static_resources', 15, 1 );
add_filter( 'style_loader_src', 'remove_query_strings_from_static_resources', 15, 1 );

3. Server-Side Configuration (Advanced):

  • If you have access to server-side configuration, you can set up caching rules to serve static files without version numbers. Specific instructions vary depending on your server setup.

Important Considerations:

  • Caching Impact: Removing version numbers can affect browser caching and content delivery networks (CDNs). It might be necessary to clear cached files or adjust CDN settings after making changes.
  • Troubleshooting: If you encounter issues after removing version numbers, try temporarily reverting the changes to isolate problems.
  • Plugin Updates: Maintain plugin compatibility by checking if updates affect version number removal features.
  • Best Practices:
    • Use plugins or code snippets from reputable sources.
    • Test changes on a staging site before implementing them on your live site.
    • Create backups of your website files and database before making modifications.

By carefully choosing the method that aligns with your technical expertise and website setup, you can effectively remove version numbers from CSS and JS files in WordPress. Remember to prioritize security and compatibility throughout the process.

Al Mahbub Khan
Written by Al Mahbub Khan Full-Stack Developer & Adobe Certified Magento Developer

Full-stack developer at Scylla Technologies (USA), working remotely from Bangladesh. Adobe Certified Magento Developer.

Leave a Reply

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