如何将自定义css和js文件添加到wordpress的管理区域


How do i add custom css and js files to admin area of wordpress

这个问题被问了太多次,有太多不同的问题,出于某种原因,我似乎找不到任何答案。非常奇怪的是,我早些时候就开发了主题,而且它正在发挥作用。

我需要为我的主题做一些选项,所以我在wordpress管理区做了单独的菜单,它很有效,我在那个页面上添加了一些选项,它会显示出来,保存选项也很有效。

现在,我想制作更多的选项,并使它们与jquery ui选项卡一起使用。我知道wordpress现在本机支持jqueryui,但它需要额外调用才能加载。

因此,在对代码进行了太多的处理之后,我最终从generatews.com网站上提取了一个应该可以工作的代码,但它不起作用,为什么我不能理解这一点。

现在的代码是:

function custom_styles() {
wp_register_style( 'jquery-ui-style', 'http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css', false, false );
}
// Hook into the 'admin_enqueue_scripts' action
add_action( 'admin_enqueue_scripts', 'custom_styles' );

// Register Script
function custom_scripts() {
wp_register_script( 'jquery-ui-core', '', array( 'jquery' ), false, false );
wp_register_script( 'jquery-ui-tabs', '', array( 'jquery' ), false, false );
}
// Hook into the 'admin_enqueue_scripts' action
add_action( 'admin_enqueue_scripts', 'custom_scripts' );

根据wordpress的记录,这应该注册jquery ui标签和样式,但它没有。

我尝试了许多其他组合,但都不起作用。为什么我不能理解。

直接来自codex@admin_enque_scripts有一些特定的挂钩可以做你想做的事情。

function my_enqueue($hook) {
    if( 'edit.php' != $hook )
        return;
    wp_enqueue_script( 'my_custom_script', plugins_url('/myscript.js', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

function load_custom_wp_admin_style() {
        wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
        wp_enqueue_style( 'custom_wp_admin_css' );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );