2011-11-30 32 views
0

我想在我開發的wordpress插件中使用css和js。我看了一下codex,我知道我必須使用wp_enqueue_script來添加js,並且wp_enqueue_style可以用plugins_url添加css來輸出正確的路徑,但我無法弄清楚如何去做。我有一個文件結構,以便它是插件,然後是插件> css和插件> js,其中css和js是包含其各自文件的文件夾。使用js和css用wordpress插件

我試着這樣做:

function my_scripts_method() { 
    wp_enqueue_script('js/tabpane.js'); 
    wp_enqueue_script('local/helptip.js'); 
    wp_enqueue_script('local/webfxapi.js'); 
    wp_enqueue_script('local/webfxlayout.js'); 
} 

add_action('wp_enqueue_scripts', 'my_scripts_method'); // For use on the Front end (ie. Theme) 

,但它不工作,和食品是不是更多的幫助。任何人都可以讓我知道我錯過了什麼嗎?

回答

0

當將文件添加到後臺插件,你需要在代碼中做幾件事情:

add_action('init', 'add_main_javascript'); 

function add_main_javascript(){ 
    $slug = "main"; 
    $file = "inc/js/$slug.js"; 
    include_js_files($file, $slug); 
} 

function include_js_files($file, $slug, $in_footer = NULL){ 
    if(!$file) 
    $file = "inc/js/filename.js"; 
    $your_plugin_dir = plugin_dir_path(__FILE__); 
    $your_plugin_url = plugin_dir_url(__FILE__); 
    if(file_exists($your_plugin_dir.$file)) 
    // array('jquery') is the scope of the javascript 
    // Set $in_footer to true if you need the js to be in the footer 
    wp_enqueue_script("filename.js", $your_plugin_url.$file, array ('jquery'), "", $in_footer); 
} 

或者你可以只使用ADD_ACTION(「初始化」,「your_code_for_enqueue」);