开发者

WordPress Problem with wp_enqueue_script

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.开发者_如何学Go


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.


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.


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


Change your code to:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜