views:

305

answers:

4

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'&gt;&lt;/script&gt; 

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

+2  A: 

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.

toscho
A: 

Hi, in the above answer, what do you mean by 'script_loader_src' ? I cant quit get how to remove that blody ?ver=2.9.2 thing, it is killing me please help!!!!!

maxim
A: 

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'.

Antonio