Using WordPress ‘add_utility_page()’ PHP function

The add_utility_page() WordPress PHP function is used to add a top-level menu page in the ‘utility’ section. This function requires a capability which determines if a page is included in the menu. The handling function for the page output must also verify that the user has the required capability.

Usage

Here’s a basic use case for the add_utility_page() function:

add_utility_page(
    'My Utility Page',
    'Utility Page',
    'manage_options',
    'my-utility-page',
    'my_custom_function',
    'dashicons-admin-tools'
);

In this example, we’re creating a new utility page with the title ‘My Utility Page’. The menu title is ‘Utility Page’, and ‘manage_options’ is the capability required for this menu to be displayed to a user. The ‘my-utility-page’ is the unique slug for this menu. The ‘my_custom_function’ will handle the output of this page. The last parameter is an optional one, providing an icon URL, and in this case, we’re using ‘dashicons-admin-tools’.

Parameters

  • $page_title (string – Required): The text displayed in the title tags of the page when the menu is selected.
  • $menu_title (string – Required): The text used for the menu.
  • $capability (string – Required): The capability required for this menu to be displayed to the user.
  • $menu_slug (string – Required): The unique slug name for this menu.
  • $callback (callable – Optional): The function to output the content for this page. Defaults to ”.
  • $icon_url (string – Optional): The URL for the icon used for this menu. Defaults to ”.

More information

See WordPress Developer Resources: add_utility_page()

Examples

Basic Usage

Adding a basic utility page to the menu.

function my_custom_function(){
    echo 'Hello, this is my utility page!';
}

add_utility_page('My Utility Page', 'Utility Page', 'manage_options', 'my-utility-page', 'my_custom_function');

In this example, a utility page titled ‘My Utility Page’ is added to the menu. ‘my_custom_function’ is called to output the content when this page is accessed.

Adding an Icon

Adding a utility page with a custom icon.

function my_custom_function(){
    echo 'Hello, this is my utility page with a custom icon!';
}

add_utility_page('My Utility Page', 'Utility Page', 'manage_options', 'my-utility-page', 'my_custom_function', 'dashicons-admin-tools');

This is similar to the first example, but an icon is also added to the menu item using ‘dashicons-admin-tools’.

No Callback Function

Adding a utility page without a callback function.

add_utility_page('My Utility Page', 'Utility Page', 'manage_options', 'my-utility-page');

In this example, a utility page is added, but no function is specified for outputting content.

Using a Custom Capability

Creating a utility page with a custom user capability.

function my_custom_function(){
    echo 'Hello, this is my utility page for editors!';
}

add_utility_page('My Utility Page', 'Utility Page', 'edit_posts', 'my-utility-page', 'my_custom_function');

In this example, the utility page will only display to users with the ‘edit_posts’ capability.