0

I'm loading a custom script file using an enqueue and here's the output:

<script type='text/javascript' src='.../wp-content/themes/enfold-child/js/custom_script.js?ver=4.5.1'></script>

curious why it's appending that "?ver=4.5.1" at end. When I remove in console file loads fine, but when it's there blank file. Not sure if that's a caching thing or I should be loading some other way. Here's my enqueue code:

add_action('wp_enqueue_scripts','enqueue_our_required_stylesheets');

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/js/custom_script.js',
        array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
  • if my answer is a working solution you can accept my answer :) I appreciate it. – Jarmerson May 5 '16 at 18:20
3

I agree with @Mark Kaplun - what you're describing is an odd behavior though. Try this markup to override the $ver param.

wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), null, true);

https://developer.wordpress.org/themes/basics/including-css-javascript/

Alternatively, to work around the version cache try this markup which will generate a unique version with each page load.

wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), rand(1, 100), true);

0

You should always specify a version number for you scripts/css when you enqueue them, otherwise when you change them on the server they might not be changed for the user because some caching proxy in the middle cached it and keep serving it.

If you don't specify one, wordpress will attach its own version number to the url as the ver parameter, but frankly IMO this behavior is more of a bug then a feature, since obviously wordpress upgrade almost never align with changes you are doing in your JS/CSS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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