我正在学习Wordpress,我想为我的插件的特定管理页面加载自定义CSS。我在Wordpress Plugin API上阅读并做了类似的事情:
//I ADD MY OPTION PAGES
add_action( 'admin_menu', 'my_plugin_menu' );
function my_plugin_menu() {
add_menu_page( 'My option page', 'My plugin', 'manage_options', 'my-fist-slug', 'my_first_func', 'dashicons-star-empty');
add_submenu_page('my-fist-slug', 'General Setting', 'General', 'manage_options', 'my-fist-slug', 'my_first_func');
add_submenu_page('my-fist-slug'', 'Some else options', 'Some options', 'manage_options', 'my-second-slug', 'my_second_func');
}
我只是想为我的选项页面加载我的CSS,然后我这样做:
add_action('admin_enqueue_scripts', 'ln_reg_css_and_js');
function ln_reg_css_and_js($hook)
{
if($hook != 'my-first-slug'){
return;
}
wp_enqueue_style('boot_css', plugins_url('inc/bootstrap.css',__FILE__ ));
wp_enqueue_script('boot_js', plugins_url('inc/bootstrap.js',__FILE__ ));
wp_enqueue_script('ln_script', plugins_url('inc/main_script.js', __FILE__), ['jquery'], false, true);
}
这与WP codex具有相同的代码。但是,我不知道如何定义$hook
,结果是我的自定义css没有被加载。任何人都可以教我如何做到这一点?
答案 0 :(得分:1)
add_action('admin_enqueue_scripts', 'ln_reg_css_and_js');
function ln_reg_css_and_js($hook)
{
$current_screen = get_current_screen();
if ( strpos($current_screen->base, 'my-fist-slug') === false) {
return;
} else {
wp_enqueue_style('boot_css', plugins_url('inc/bootstrap.css',__FILE__ ));
wp_enqueue_script('boot_js', plugins_url('inc/bootstrap.js',__FILE__ ));
wp_enqueue_script('ln_script', plugins_url('inc/main_script.js', __FILE__), ['jquery'], false, true);
}
}
答案 1 :(得分:1)
function load_custom_wp_admin_style($hook) {
// $hook is string value given add_menu_page function.
if($hook != 'toplevel_page_mypluginname') {
return;
}
wp_enqueue_style( 'custom_wp_admin_css', plugins_url('admin-style.css', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
有关详情,请参阅:Codex examples
答案 2 :(得分:0)
您也可以在特定的插件页面上包含CSS:)
//Enqueue Admin CSS on Job Board Settings page only
if ( isset( $_GET['page'] ) && $_GET['page'] == 'job-board-settings' ) {
// Enqueue Core Admin Styles
wp_enqueue_style( simple_job_board, plugin_dir_url( __FILE__ ) . 'css/simple-job-board-admin.css');
}