为WordPress后台 Settings 顶级栏目创建一个子栏目
语法结构
<?php
add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);
?>
参数
$page_title
$menu_title:菜单名称
$capability:权限
$menu_slug
$function 回调函数
<?php
/** 第1步:定义添加菜单选项的函数 */
function my_plugin_menu() {
add_options_page(
'My Plugin Options',
'My Plugin',
'manage_options',
'my-unique-identifier',
'my_plugin_options' );
}
/** 第2步:将函数注册到钩子中 */
add_action( 'admin_menu', 'my_plugin_menu' );
/** 第3步:定义选项被点击时打开的页面 */
//current_user_can()检测当前的用户是否有特定的权限
//wp_die()终断WordPress执行并显示错误HTML信息。
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( 'You do not have sufficient permissions to access this page.' );
}
echo '<div class=wrap>Here is where the form would go if I actually had options.</div>';
}
?>
为其他顶级栏目创建子菜单的函数
Dashboard
<?php add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Posts
<?php add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Media
<?php add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Links
<?php add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Pages
<?php add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Comments
<?php add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Appearance
<?php add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Plugins
<?php add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Users
<?php add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
Tools
<?php add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
发表评论