2012-12-27 44 views
1

我正在使用thickbox和media-upload來設置一些圖像的插件。也不會加載使用此代碼:爲什麼WordPress不會加載thickbox和媒體上傳腳本?

function add_my_files() { 
     echo 'happy happy happy'; 
     wp_register_style('adminstyles', plugins_url('/css/slider.css', __FILE__)); 
     wp_enqueue_style('adminstyles'); 
     wp_enqueue_style('thickbox'); 

     wp_enqueue_script('media-upload'); 
     wp_enqueue_script('thickbox'); 

     wp_register_script('hdjs',plugins_url('/js/slider.js', __FILE__),array('media-upload','thickbox'),'',true); 
     wp_enqueue_script('hdjs'); 
    } 

add_action('admin_init', 'add_my_files'); 

我的css和js文件加載但不thickbox和媒體上傳。

感謝

+0

「不加載」意味着什麼 - 他們實際上,物理上沒有加載?或者他們不工作*?例如, –

+0

thickbox.js沒有物理加載。 – dcp3450

+0

它沒有加載,或者在HTML文件中根本沒有任何參考? –

回答

1

正確的掛鉤,包括在WP你的資源文件是admin_enqueue_scripts

注:我建議你也使用get_current_screen(見下文is_my_admin_screen()定義)只包括你的JS/CSS文件,當您實際上需要。

add_action('admin_enqueue_scripts', 'add_my_files'); 

function add_my_files() 
{ 
    /* 
    * a good WP citizen only loads 
    * their javascript/css where it is needed 
    */ 
    if (! is_my_admin_screen()) // defined below 
     return; 

    wp_register_style('adminstyles', plugins_url('/css/slider.css', __FILE__)); 
    wp_enqueue_style('adminstyles'); 
    wp_enqueue_style('thickbox'); 

    wp_enqueue_script('media-upload'); 
    wp_enqueue_script('thickbox'); 

    wp_register_script('hdjs', plugins_url('/js/slider.js', __FILE__), array('media-upload', 'thickbox'), '', true); 
    wp_enqueue_script('hdjs'); 
} 

function is_my_admin_screen() 
{ 
    $screen = get_current_screen(); 

    if (is_object($screen) && $screen->id == 'my_plugin_page_id') // var_dump($screen->id); find your own id 
     return true; 
    else 
     return false; 
} 

裁判:http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts REF:http://codex.wordpress.org/Function_Reference/get_current_screen


而且希望您使用類來包裝所有的插件或您將有比這更糟糕的問題。

請反饋意見。我對這個問題很感興趣,因爲WP插件把食物和啤酒放在我的桌子上。

相關問題