我正在尝试设计插件的管理页面。下面是使用的代码,但样式表和脚本都没有显示在我的页面上。
add_action( \'admin_init\', \'myplugin_admin_init\' );
add_action( \'admin_menu\', \'myplugin_custom_menu_page\' );
function myplugin_custom_menu_page(){
$page = add_menu_page( \'My Plugin\', \'My Plugin\', \'manage_options\', \'myplugin/myplugin-admin.php\', \'\', plugins_url( \'myplugin/images/icon.png\' ), 33 );
add_action( \'admin_enqueue_scripts\' . $page, \'myplugin_admin_styles\' );
add_action( \'admin_enqueue_scripts\' . $page, \'myplugin_admin_scripts\' );
}
function myplugin_admin_init() {
wp_register_script( \'myplugin-script\', plugins_url( \'myplugin/script.js\') );
wp_register_style( \'myplugin-style\', plugins_url(\'myplugin/stylesheet.css\') );
}
function myplugin_admin_styles() {
wp_enqueue_style( \'myplugin-style\' );
}
function myplugin_admin_scripts() {
wp_enqueue_script( \'myplugin-script\' );
}
任何提示都将不胜感激!
SO网友:Eugene Manuilov
仅在插件页面上添加管理脚本/样式的另一种方法:
//The add_action to add onto the WordPress menu.
add_action(\'admin_menu\', \'myplugin_custom_menu_page\');
function myplugin_custom_menu_page() {
$GLOBALS[\'my_plugin_page\'] = add_menu_page( \'My Plugin\', \'My Plugin\', \'manage_options\', \'myplugin/myplugin-admin.php\', \'\', plugins_url( \'myplugin/images/icon.png\' ), 33 );
}
add_action( \'admin_enqueue_scripts\', \'my_enqueue_scripts\' );
function my_enqueue_scripts($hook_suffix) {
if( $GLOBALS[\'my_plugin_page\'] == $hook_suffix ) {
wp_enqueue_style( \'myplugin-admin\', plugins_url(\'stylesheet.css\', __FILE__) );
wp_enqueue_script( \'myplugin-admin\', plugins_url(\'script.js\', __FILE__) );
}
}
在这种方法中,您不必关注屏幕名称。您将始终仅为插件页面将脚本排队。