0

I am working on my first plugin that adds a external script to the footer. Wordpress automatically adds the version number at the end, which I'm trying to remove. What I have so far:

add_action('wp_enqueue_scripts', array($this,'print_code'));

public function print_code(){
         
        $location   =   get_option('location_select');
        $url        =   'https://' . esc_html($location) . '.domain.com/js/script.js';
        wp_enqueue_script('myID', $url, array('jquery'), false, true);


     }

This puts out the script correctly, but I still have the version (?ver=5.8) at the end. The false should avoid that, afaik. What am I missing?

2
  • 1
    Take a look at this post to see if it helps: How to remove the Wordpress version from some .css/.js files
    – ScottM
    Jul 21, 2021 at 17:31
  • 2
    That'll work if you need to remove the version from scripts and stylesheets outside your control (ie, loaded by core or 3rd-party themes/plugins), but for OP's question it's as simple as passing null as the 4th ($version) parameter. See the docs.
    – Pat J
    Jul 21, 2021 at 18:26

2 Answers 2

4

Change the 4th parameter from false to null.

wp_enqueue_script( 'myID', $url, array( 'jquery' ), null, true );

From the wp_enqueue_script() documentation:

$ver
(string|bool|null) (Optional) String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.

(emphasis added)

1
  • 1
    Thank you for your quick answer! null works of course, I did not read the docs properly -.-
    – Paranoia
    Jul 22, 2021 at 6:30
3

As defined in the codex,

If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.

You're currently setting false. You should be setting null:

wp_enqueue_script('myID', $url, array('jquery'), null, true);
1
  • Thank you for the quick answer! Looks like I am not able to read -.- I set it to null and it works!
    – Paranoia
    Jul 22, 2021 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.