WP: how to remove version number in wp_enqueue_script?
Asked Answered
H

4

8

I'm trying to remove the version number from appearing in the URL generated by wp_enqueue_script. It seems like I should pass a null on the 4th parameter per http://codex.wordpress.org/Function_Reference/wp_enqueue_script:

wp_enqueue_script('jquery', false, array(), null, false);

It's not working. I still see the version number. How do I remove that?

Also, how do I use wp_enqueue_script so that I get jQuery from the Google CDN?

Hindi answered 25/2, 2013 at 7:35 Comment(0)
C
16

You can either use

wp_enqueue_script('jquery', 'URL', array(), '', false);

or

wp_enqueue_script('jquery', 'URL', array(), null, false);

or you can put a generic name placeholder

wp_enqueue_script('jquery', 'URL', array(), 'custom', false);

But particularly with "jquery" I would deregister the default if you want to replace it

wp_deregister_script('jquery');
$GoogleJqueryURI = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
wp_register_script('jquery', $GoogleJqueryURI, array(), 'custom', false);
wp_enqueue_script('jquery');
Centri answered 25/2, 2013 at 7:43 Comment(4)
What does this "custom" do?Hindi
This worked for me: wp_register_script('jquery', $GoogleJqueryURI, array(), null, false);. It need the URL not to contain a query string. Putting "custom" resulted in something like this: ?ver=custom, which is not what I wanted.Hindi
custom doesn't do anything. It is just a word placeholder for the Version. Basically would show ?ver=custom where << version does not technically have to show an actual version number.Centri
'' won't work, null worked. Btw never use the external jquery version with WordPress, it will cause lots of conflict with inbuild WordPress jQueryMalia
H
1

try something like this:

wp_deregister_script('jquery'); 
wp_register_script('jquery', ('http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js'), false, NULL);
wp_enqueue_script('jquery');

The $ver and $in_footer are optional. So just leave them off.

Also, if you use the google cdn then it will be hard to hide the version any way, its in the url.

Hamrick answered 25/2, 2013 at 7:43 Comment(1)
Yes, $ver is optional. But the default behavior is that it appears in the URL produced. I don't want the the query string. As for the Google CDN, it doesn't have the version in the query string, so it's fine.Hindi
T
0
function remove_css_js_version( $src ) {
    if( strpos( $src, '?ver=' ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'remove_css_js_version', 9999 );
add_filter( 'script_loader_src', 'remove_css_js_version', 9999 );
Tourism answered 21/12, 2020 at 8:17 Comment(2)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From ReviewMixup
This is not proper solution as you need to use correct value for variable #ver.Sharenshargel
S
0

An example when version is not added.

wp_enqueue_script('element-plus', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/index.full.min.js', null, null)

An output of code above will be:

<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/index.full.min.js' id='element-plus-js'></script>

According to documentation several variables are accepted:

wp_enqueue_script( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

If value for $ver is false (or not given), then automatically version of WP is used.

Sharenshargel answered 6/9, 2021 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.