2012-01-09 156 views
1

在我的插件中,我需要包含1个CSS和2个js文件(jquery.min.js和jquery.googleplus-activity-1.0.min.js)。如何在Wordpress中包含jQuery插件?

我使用这个代码

function my_init_method() 
{ 
    wp_deregister_script('jquery'); 
    wp_enqueue_script('jquery', '' . get_bloginfo('wpurl') . '/wp-content/plugins/googe_plus/js/jquery.min.js'); 
} 

function addHeaderCode() { 
    echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/googe_plus/css/style.css" />' . "\n"; 

} 


function my_jsscripts_method() { 

    wp_deregister_script('jquery'); 
    wp_register_script('jquery', '' . get_bloginfo('wpurl') . '/wp-content/plugins/googe_plus/js/jquery.googleplus-activity-1.0.min.js'); 
    wp_enqueue_script('jquery'); 
} 
add_action('init', 'my_init_method'); 
//Include CSS 
add_action('wp_head', 'addHeaderCode'); 

//Include JS 
add_action('wp_enqueue_scripts', 'my_jsscripts_method'); 

但不能js文件的添加两者。 请记住,我必须包括jquery.min.js第一则CSS和其他js文件jquery.googleplus活动,1.0.min.js

+0

不回答你的问题,但请注意,其他插件也可能使用jQuery。在添加JS之前检查jQuery是否存在。 – Raptor 2012-01-09 09:01:11

+0

@ShivanRaptor是的我知道,但我认为全球可能使用不使用它,所以我必须包括它 – 2012-01-09 09:03:37

回答

2

我会使用一个不同的句柄jquery.googleplus-activity-1.0.min.js文件。你可能会混淆WordPress试图重用“jquery”。因此,像:

function my_jsscripts_method() { 
    wp_register_script('jquery.googleplus', '' . get_bloginfo('wpurl') . '/wp-content/plugins/googe_plus/js/jquery.googleplus-activity-1.0.min.js'); 
    wp_enqueue_script('jquery.googleplus'); 
} 

可能会使用第三个参数wp_register_script有它依赖于jQuery的价值。另外,除非你有很好的理由,否则我可能会使用WordPress自带的jQuery版本(而不是替换它)。您可能会遇到兼容性问题。