2012-01-09 179 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版本(而不是替換它)。您可能會遇到兼容性問題。