add_object_page()
云策文档标注
概述
add_object_page() 函数用于在 WordPress 管理后台的“对象”部分添加一个顶级菜单页面,但自 WordPress 4.5.0 起已被弃用,建议改用 add_menu_page()。该函数通过指定权限、标题和回调函数等参数来定义菜单项。
关键要点
- add_object_page() 已弃用,自 WordPress 4.5.0 起应使用 add_menu_page() 替代。
- 函数参数包括 $page_title、$menu_title、$capability、$menu_slug、$callback 和 $icon_url,其中 $callback 和 $icon_url 为可选。
- 权限检查(基于 $capability)应在处理页面输出的 PHP 文件中进行,以确保菜单链接和页面内容对用户可见。
- 如果未提供 $callback 参数,WordPress 会假设包含的 PHP 文件直接生成管理界面,此时 $menu_slug 应指向该文件。
- 函数返回生成的页面的 hook_suffix。
代码示例
function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '') {
_deprecated_function( __FUNCTION__, '4.5.0', 'add_menu_page()' );
global $_wp_last_object_menu;
$_wp_last_object_menu++;
return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $_wp_last_object_menu);
}注意事项
- 使用此函数时,WordPress 会标记为弃用并提示使用 add_menu_page()。
- 确保 $menu_slug 唯一,以避免菜单冲突。
- 在回调函数或 PHP 文件中实现权限验证,以匹配 $capability 参数。
原文内容
Add a top-level menu page in the ‘objects’ section.
Description
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.
See also
Parameters
$page_titlestringrequired-
The text to be displayed in the title tags of the page when the menu is selected.
$menu_titlestringrequired-
The text to be used for the menu.
$capabilitystringrequired-
The capability required for this menu to be displayed to the user.
$menu_slugstringrequired-
The slug name to refer to this menu by (should be unique for this menu).
$callbackcallableoptional-
The function to be called to output the content for this page.
$icon_urlstringoptional-
The URL to the icon to be used for this menu.
Source
function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '') {
_deprecated_function( __FUNCTION__, '4.5.0', 'add_menu_page()' );
global $_wp_last_object_menu;
$_wp_last_object_menu++;
return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $_wp_last_object_menu);
}
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Deprecated. Use add_menu_page() |
| 2.7.0 | Introduced. |