函数文档

add_utility_page()

💡 云策文档标注

概述

add_utility_page() 函数用于在 WordPress 管理后台的“实用工具”部分添加一个顶级菜单页面。该函数自 WordPress 4.5.0 版本起已被弃用,建议使用 add_menu_page() 替代。

关键要点

  • 函数功能:在“utility”区域创建顶级菜单页面,需指定页面标题、菜单标题、权限、菜单 slug 等参数。
  • 弃用状态:自 WordPress 4.5.0 起被标记为弃用,应改用 add_menu_page() 函数。
  • 权限控制:函数基于 $capability 参数控制菜单显示,回调函数需额外验证用户权限。
  • 返回值:返回页面的 hook_suffix,可用于后续操作。

代码示例

function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '') {
    _deprecated_function( __FUNCTION__, '4.5.0', 'add_menu_page()' );

    global $_wp_last_utility_menu;

    $_wp_last_utility_menu++;

    return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $_wp_last_utility_menu);
}

注意事项

  • 此函数已弃用,新开发中应避免使用,改用 add_menu_page()。
  • 回调函数必须检查用户是否具有指定的 $capability,以确保安全访问。
  • 菜单 slug 应保持唯一性,以避免冲突。

📄 原文内容

Add a top-level menu page in the ‘utility’ 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.

Return

string The resulting page’s hook_suffix.

Source

function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '') {
	_deprecated_function( __FUNCTION__, '4.5.0', 'add_menu_page()' );

	global $_wp_last_utility_menu;

	$_wp_last_utility_menu++;

	return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $_wp_last_utility_menu);
}

Changelog

Version Description
4.5.0 Deprecated. Use add_menu_page()
2.7.0 Introduced.