5

I try to us wp_enqueue_script to load my javascript, here is my code:

<?php wp_enqueue_script('slider','/wp-content/themes/less/js/slider.js',array('jquery'),'1.0'); ?>

It's not working, when I look into the source, it turns to be:

<script type='text/javascript' src='http://localhost/wp/wp-content/themes/less/js/slider.js?ver=2.9.2'></script> 

?ver=2.9.2 is added to the end automatically, I guess this is the reason, how can I fix it.

5
  • @Zack by adding at the end ?ver=2.9.2 ? Commented Mar 25, 2010 at 8:57
  • ?ver=2.9.2 is automatically added to the end Commented Mar 25, 2010 at 8:59
  • @c0mrade, I tried to add ?ver=2.9.2, to the link, make it: /wp-content/themes/less/js/slider.js?ver=2.9.2 it works, as the result it become: <script type='text/javascript' src='localhost/wp/wp-content/themes/less/js/…> so this is the correct way, right? Commented Mar 25, 2010 at 9:03
  • Thanks to c0mrade, I wish you tell me what causes that Commented Mar 25, 2010 at 9:14
  • WordPress appears to append the version of WordPress installed. I've also tried this and today's current version of WordPress (?ver=3.3.2) was appended to the uri when my parameters weren't following syntax. Commented May 18, 2012 at 23:57

4 Answers 4

9

Wordpress's documentation is poorly documented in this regard.

Change from false to null in the second last parameter to remove ?ver=2.9.2.

Sign up to request clarification or add additional context in comments.

Comments

5

To remove the version parameter you need an extra filter. This is how I use Google’s jQuery without a query string:

<?php
// Use the latest jQuery version from Google
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false, false);
wp_enqueue_script('jquery');

add_filter('script_loader_src', 'toscho_script_loader_filter');

function toscho_script_loader_filter($src)
{
    if ( FALSE === strpos($src, 'http://ajax.googleapis.com/') )
    {
        return $src;
    }
    $new_src = explode('?', $src);

    return $new_src[0];
}
?>

You may even use the last filter to add your own query vars.

Usually the query string should not affect your script. I remove it just to increase the probability that the user can use a cached version of this file.

3 Comments

toscho, how can I adapt your script to an add_action function that takes a custom "jquery include" function? the custom function just does what you did int he first two lines and without the wp_enqueue_script at the end.
@ilia, sorry, I really don’t understand your question. Maybe you should open a new question and show the code you’re working on.
yeah I had a feeling it wouldn't be very clear but I was hoping to avoid creating a new question because it is in fact a very similar situation and yet different enough that I'm still having trouble with it. Anyway, I created a question for it here stackoverflow.com/questions/5256025/…
2

You can use null as the fourth parameter if you are using Wordpress 3.0.This will effectively remove the version.

Comments

1

Change your code to:

<?php wp_enqueue_script('slider','/wp-content/themes/less/js/slider.js',array('jquery'),null); ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.