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

时间:2013-08-31 23:01:10

标签: php javascript jquery css wordpress

问题被问了太多次,并且有太多不同的问题,并且出于某种原因,我似乎无法使用我找到的答案。非常奇怪的是,我之前已经开发了主题并且它正在发挥作用。

我需要为我的主题制作一些选项,所以我在wordpress管理区域中制作了单独的菜单并且它可以工作,我为该页面添加了一些选项并显示了它,同时也保存了选项。

现在我想制作更多选项,并使用jquery ui制表符标记它们。我知道wordpress本身支持jquery ui,但需要额外调用才能加载。

所以在用最后的代码弄乱了太多代码之后我从generatewp.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标签和样式,但它不会。

我尝试了很多其他组合,它根本不起作用。为什么我无法理解。

1 个答案:

答案 0 :(得分:1)

直接来自codex @ admin_enqueue_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' );