最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WordPress Problem with wp_enqueue_script - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Mar 25, 2010 at 8:59 Zack asked Mar 25, 2010 at 8:53 ZackZack 3971 gold badge8 silver badges16 bronze badges 5
  • @Zack by adding at the end ?ver=2.9.2 ? – ant Commented Mar 25, 2010 at 8:57
  • ?ver=2.9.2 is automatically added to the end – Zack 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? – Zack Commented Mar 25, 2010 at 9:03
  • Thanks to c0mrade, I wish you tell me what causes that – Zack 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. – user1108579 Commented May 18, 2012 at 23:57
Add a comment  | 

4 Answers 4

Reset to default 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.

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)

  1. 暂无评论