我正在构建一个 WordPress 插件,但在管理菜单的插件选项页面上加载 css 时遇到问题。我在http://codex.wordpress.org/Function_Reference/wp_enqueue_style查阅了法典
这就是我得到的(我通过删除与我的问题无关的所有部分并更改插件名称来简化代码):
<?php
if (!class_exists('My_Plugin')){
class My_Plugin{
public function __construct(){
//add admin page
add_action('admin_menu', array($this, 'my_plugin_menu'));
add_action('admin_init', array($this, 'my_admin_init'));
}
//adds a page in the admin menu under posts
public function my_admin(){
//ensure user is admin
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>My Plugin</h1>
</div>
<?php
}
//admin initialization function
public function my_admin_init(){
//register admin page's css
wp_register_style('my_style', plugins_url('my_style.css', __FILE__));
}
//add css files for admin menu()
public function my_admin_enqueue_styles(){
wp_enqueue_style('my_style');
}
//add menu page under posts
public function my_plugin_menu(){
$page = add_submenu_page('edit.php', 'My Plugin', 'My Plugin', 'manage_options', 'my-plugin', array($this, 'my_admin'));
//add css
add_action('admin_print_styles-'.$page, 'my_admin_enqueue_styles');
}
}
//instantiate the class
$mp = new My_Plugin();
}
在 my_style.css 文件中,我只是h1{color:red;}
为了测试它而卡住了。选项页面加载正常,但萤火虫根本没有显示 css 正在加载。我难住了。我需要做什么?