• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero Blog

NameHero Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • VPS Hosting
    • WordPress Hosting
    • WooCommerce Hosting
  • Reseller
  • Enterprise
  • Domains
  • Account
  • Blog Home
  • Categories

How To Remove The Version From wp_enqueue_style In WordPress

Bhagwad Park

Published on: April 25, 2022

Categories: WordPress 0

I’ve written about how to add custom CSS to WordPress. Instead of modifying your theme’s style.css file, creating a child theme, or using a plugin, I suggest you simply create a new stylesheet with the additional CSS rules and include them in WordPress using the wp_enqueue_style function. This way, your custom CSS rules are independent of your theme and you don’t have to worry about updates, and you don’t have to bother with creating a child theme either.

WordPress Appends a Version Number

However, if you use the default wp_enqueue_style function, WordPress will append a version number to the end. Let’s say you want to enqueue a CSS file called “custom-css.css” and use the following code:

function replace_some_default_styles() {
   wp_enqueue_style( 'three_column', get_stylesheet_directory_uri() . '/custom-css.css');
 }
add_action( 'wp_enqueue_scripts', 'replace_some_default_styles' );

After adding this code either to your functions.php (not recommended), or to the custom plugin holding your code, this CSS file will be included on your site like this:

File Enqueued in WordPress with the Version Number
File Enqueued in WordPress with the Version Number

As you can see, while the file now appears in the source code, there’s an annoying version number attached to the end. In this case: ?ver=5.9.3 . This is the current WordPress version number and it’s appended by default to all styles enqueued using wp_enqueue_style. The same goes for scripts enqueued using wp_enqueue_script. When the WordPress version changes, this version number changes as well.

Removing the Version Number

When WordPress appends a version number to styles and scripts like this, it can cause all sorts of problems – particularly those involving caching. I think it’s a good idea to instruct your CDN to ignore query strings, since there are other, more efficient ways to cache bust, and having a version number just introduces an additional layer of complexity.

Fortunately, removing the version number from the function is easy. We just need to modify this:

wp_enqueue_style( 'three_column', get_stylesheet_directory_uri() . '/custom-css.css');

into this:

wp_enqueue_style( 'three_column', get_stylesheet_directory_uri() . '/custom-css.css',array(), null );

So the entire block of code becomes:

function replace_some_default_styles() {

   wp_enqueue_style( 'three_column', get_stylesheet_directory_uri() . '/custom-css.css',array(), null );
  
}
add_action( 'wp_enqueue_scripts', 'replace_some_default_styles' );

Simple! Now after you save your changes, you can confirm that the version number of the style or script is no longer a part of the URL.

Instructing Cloudflare to Ignore Query Strings

It’s a good idea to tell your CDN to ignore all query strings for static resources. This way, you don’t have to deal with multiple versions of files, which can cause all kinds of debugging issues when you make changes. Caching issues can be quite hard to isolate, especially if you have multiple layers of caching, starting from your server, and then on a CDN.

For Cloudflare, switching off the caching of query strings is easy. Just locate the “Caching” tab on your dashboard, and under configuration, select the option to ignore the query string like this:

Cloudflare Caching Level
Cloudflare Caching Level

Just a simple change and you’re done!

Save Auto-Versioning Headaches

Some developers use workarounds to ensure that they’re always using the latest version of CSS or JavaScript files. Auto-versioning is a popular hack that uses the “Last Modified” timestamp on a file to create a version number that’s unique to that particular file. So when it changes, you don’t need to worry about emptying various caches. It’s a neat trick, and I see how it has its benefits.

But I still worry that it adds an extra layer of complexity to your code and that it’s easy for someone to forget about it and for it to rear its head in unexpected situations later on. Far easier to just disable caching temporarily in production and then enable it when the site is ready to go live.

Remember – keep it simple!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!

Most Popular Posts

Speed up your site: solving the WordPress slowdown

NameHero’s Recommended WordPress Plugin and Theme Setup (2023)

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • Website Performance
  • Web Hosting
  • Resellers
  • Website Development
  • Website Security
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Domains
Help & Support
  • NameHero Blog
  • Knowledgebase
  • Announcements
  • Affiliates
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2023 NameHero, LLC. All rights reserved.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA