Get rid of ?ver on the end of CSS/JS files?
-
Hi,
I’m trying to work out how to remove the ?ver=xxx stuff from the end of file.
Is this something done in the “main” PHP code for WordPress, or is it on a per-plugin basis?
Its really annoying me, as that seems to be whats stopping my CDN from working right with those files (it never matches them!)
TIA
Andy
-
Have a look at the method i used here in Modifying an existing registered style or script.
I’m not suggesting you re-point the file(like i did there), but the hook i used there will be an appropriate place to remove the ver query parameter using something like..
$src = remove_query_arg( 'ver', $src );
Hi,
Thanks for the reply ?? (first reply I think I’ve ever had – everyone seems to ignore my posts :()
Will give that a go this evening. Thanks again!
Andy
There’s just more people asking questions than there is answering them, and people reading won’t always know the answer to what you’re asking.
Let me know how you get on… ??
haha I know the feeling. I help on a pretty large forum (gossamer threads), and I’m about the only one who really helps people – everyone else just wants to ask questions ??
I’m not very familiar with PHP/Wordpress (only started using it for my new blog, although I dabbled in PHP a while back) … my main language is Perl ??
Where would I edit the code you suggested? In the plugin files, or a main .php / .inc file?
TIA!
Andy
Get dabbling in PHP again, with a Perl background you should be able to pick it up pretty quick, in the mean time, you’re aiming for something like this..
function remove_cssjs_ver( $src ) { if( strpos( $src, '?ver=' ) ) $src = remove_query_arg( 'ver', $src ); return $src; } add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
That can be placed into your current theme’s functions.php, which will reside in something along the lines of wp-content/themes/YOURTHEMENAME/.
More information on the functions file for themes can be found here.
https://codex.www.ads-software.com/Theme_Development#Functions_FileInfo on actions/filters.
https://codex.www.ads-software.com/Plugin_APIHope that helps. ??
You’re a legend!!!!
Adding this into functions.php works a charm:
function remove_cssjs_ver( $src ) { if( strpos( $src, '?ver=' ) ) $src = remove_query_arg( 'ver', $src ); return $src; } add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 ); add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
Its removed the ?ver=xxx from both the js and css files – which is exactly what I needed/wanted
Thanks!
Andy
Oops, forgot the second hook, but looks like you caught that.. ??
You’re welcome.. ??
Hi,
Ah, doesn’t seem to be working quite right ??
https://www.mywebsitedesignersblog.com/2011/01/speeding-up-your-site-with-cdns/
If you look at the source, you will see:
<link rel="stylesheet" type="text/css" href="https://webdesign2.ultra-cdn.com/wp-content/themes/df_marine/style.css">
..which is fine (as its going through the CDN) … but with the changes I made in the functions.php, it looks like thats run AFTER the “W3 Total Cache” plugin has been run?
Cos this one *should* be using the CDN, but doesn’t seem to be ??
<link rel='stylesheet' id='jquery.lightbox.min.css-css' href='https://www.mywebsitedesignersblog.com/wp-content/plugins/wp-jquery-lightbox/lightbox.min.css' type='text/css' media='all' />
I wish the author of that plugin would just make it so the ?ver=xxx is just ignored – it seems to be very picky about the URL’s for the CSS/JS files that it passes to the CDN ??
Any suggestions are much appreciated
Cheers
Andy
The code provided will only work for enqueued styles/scripts, i’d guess at this point the given plugin is not enqueuing scripts/styles but printing them out directly(easy to confirm, remove the code i gave you, does that line then have a version appended? – if not, it’s not enqueued)..
Hi,
Thanks for the reply – sorry about my tardy response time – didn’t get an email for some reason :/
If I comment out the lines:
// add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 ); // add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
..then the general CSS file comes up as:
<link rel='stylesheet' id='jquery.lightbox.min.css-css' href='https://www.mywebsitedesignersblog.com/wp-content/plugins/wp-jquery-lightbox/lightbox.min.css?ver=1.2' type='text/css' media='all' />
TIA
Andy
BTW, in wp-jquery-lightbox.php, I see the following:
function jqlb_css(){ if(is_admin() || is_feed()){return;} wp_enqueue_style('jquery.lightbox.min.css', JQLB_STYLE_URL, false, '1.2'); }
I’m assuming thats where the problem is coming from?
TIA
Andy
I can’t verify that problem myself, installing and running the plugin, the filter correctly removes the version number from the enqueue to
jquery.lightbox.min.css
.Output from source.
<link rel='stylesheet' id='jquery.lightbox.min.css-css' href='https://myurl/wp-content/plugins/wp-jquery-lightbox/lightbox.min.css' type='text/css' media='all' />
Without filter.
<link rel='stylesheet' id='jquery.lightbox.min.css-css' href='https://myurl/wp-content/plugins/wp-jquery-lightbox/lightbox.min.css?ver=1.2' type='text/css' media='all' />
As far as my testing goes, the filter is working as expected.
EDIT: Apologies, i should of read back more of your responses.
Try raising the priority number, which in turn makes the filters run later.. like so..
function remove_cssjs_ver( $src ) { if( strpos( $src, '?ver=' ) ) $src = remove_query_arg( 'ver', $src ); return $src; } add_filter( 'style_loader_src', 'remove_cssjs_ver', 1000 ); add_filter( 'script_loader_src', 'remove_cssjs_ver', 1000 );
This will hook the filter on at a later point, and hopefully resolve the problem.
Hi,
Sorry, I must have not explained myself properly ??
The “mod” *does* work – but not quite how I’m wanting =)
I have the “W3 Total Cache” plugin installed, and have a CDN setup with MaxCDN. The main files, such as the theme CSS / JS files work correctly (they are accessed from the CDN)
However, the other files (CSS and JS) don’t seem to be.
This is only a problem, as I’m trying to get ALL the JS/CSS files served from my blog, to work from the CDN (to make the pages as fast as possible)
However, it seems that the plugin doesn’t wanna pick up those files for some reason (I just assumed it was to do with the ?ver=xxx stuff at the end of the files, which was causing some of the code in the Total Cache plugin not to pick those files up)
I tried having a look through the contents of that plugin, but its got sooo many features, I quickly got lost (and seeing as my PHP is very rusty, I’m wasn’t even 100% sure what I was looking for – appart from a preg_match() or similar that extracts the CSS files from the template, and then checks if they are uploaded to the CDN or not)
TIA
Andy
Hi Andy,
Did you catch my edit above regarding the filter priority, slipped that in, in the last moment.
Hi,
Nah, missed that ??
Have done that change, and the ?ver=xxx is being removed ok, but still doesn’t seem to wanna pass through the CDN ??
Will leave it an hour or so, just in case it needs time to upload to the CDN (not 100% sure how the plugin works … so maybe it has uploaded the file, but cos it obviously has to propigate to all the other CDN servers, then it can take a little while to do that)
Will reply in about an hour, to let you know one way or the other <G>
TIA
Andy
- The topic ‘Get rid of ?ver on the end of CSS/JS files?’ is closed to new replies.