add_options_page()

在后台管理界面的“设置”菜单下增加二级菜单

用法

 <?php
add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);
?> 

参数

$page_title

(string) (必填) 选择菜单时要在页面标题标签中显示的文本

默认值: None

$menu_title

(string) (必填) 要用于菜单的文本

默认值: None

$capability

(string) (必填) 向用户显示此菜单所需的功能。

默认值: None

$menu_slug

(string) (必填) 用于引用此菜单的 slug 名称(对于此菜单应该是唯一的)。

默认值: None

$function

(callback) (可选) 要调用以输出此页面内容的函数。

默认值: ‘ ‘

返回值

string 

生成的页面hook_suffix(add_submenu_page() 返回的内容)

示例

典型用法发生在注册了“admin_menu”钩子的函数中(请参阅添加管理菜单):

add_action(‘admin_menu’, ‘my_plugin_menu’);

function my_plugin_menu() {

add_options_page(‘My Options’, ‘My Plugin’, ‘manage_options’, ‘my-plugin.php’, ‘my_plugin_page’);

}

面向对象的选项页帮助程序/视图:

/* ———————————-
* wordpress函数 Xingkongweb.com收集
* ———————————- */

class options_page {

function __construct() {

add_action( ‘admin_menu’, array( $this, ‘admin_menu’ ) );

}

function admin_menu () {

add_options_page( ‘Page Title’,‘Circle Tree Login’,‘manage_options’,‘options_page_slug’, array( $this, ‘settings_page’ ) );

}

function  settings_page () {

echo ‘This is the page content’;

}

}

new options_page;

注意

此函数是调用 add_submenu_page() 的简单包装器,传递收到的参数并指定 'options-general.php' 作为 $parent_slug 参数。这意味着新选项页面将作为子菜单添加到“设置”菜单中。

$capability 参数用于根据当前用户的角色和功能确定页面是否包含在菜单中。

处理选项页输出的函数还应验证用户的功能。

如果 slug 中有空格,则在生成 URL 时将删除这些空格。这将导致一条错误消息,告诉您没有足够的权限来查看该页面。

源文件

add_options_page() 函数的代码位于 wp-admin/includes/plugin.php.

/**
* Add sub menu page to the options main menu.
*
* This function takes a capability which will be used to determine whether
* or not a page is included in the menu.
*
* The function which is hooked in to handle the output of the page must check
* that the user has the required capability as well.
*
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
* @param string $menu_title The text to be used for the menu
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
* @param callback $function The function to be called to output the content for this page.
*
* @return false|string The resulting page’s hook_suffix, or false if the user does not have the capability required.
*/

function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = ” ) {

return add_submenu_page( ‘options-general.php’, $page_title, $menu_title, $capability, $menu_slug, $function );

}
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索