Using WordPress ‘add_object_page()’ PHP function

The add_object_page() WordPress PHP function is used to add a top-level menu page in the ‘objects’ section. It takes a capability, which is used to determine whether a page is included in the menu or not. The function hooked to handle the output of the page must also check that the user has the required capability.

Usage

Here’s a general way to use the function:

add_object_page(
    'My Page Title', // Page Title
    'My Menu Title', // Menu Title
    'manage_options', // Capability
    'my_menu_slug', // Menu Slug
    'my_callback_function', // Callback Function
    'dashicons-admin-site' // Icon URL
);

In this example, ‘My Page Title’ will be the text displayed in the title tags of the page when the menu is selected. ‘My Menu Title’ is the text to be used for the menu. ‘manage_options’ is the capability required for this menu to be displayed to the user. ‘my_menu_slug’ is the unique slug name for this menu. ‘my_callback_function’ is the function to be called to output the content for this page. ‘dashicons-admin-site’ is the URL to the icon to be used for this menu.

Parameters

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

More information

See WordPress Developer Resources: add_object_page()
This function is included in WordPress since version 2.0.0.

Examples

Example 1

Add a page with the title ‘My Object Page’ and the menu title ‘My Object’. The ‘manage_options’ capability is required to see this menu. The ‘my_object_page_slug’ slug is used to refer to this menu. The ‘my_object_page_content’ function is used to output the content for this page.

function my_object_page_content() {
    echo 'Hello, this is my object page!';
}

add_object_page(
    'My Object Page',
    'My Object',
    'manage_options',
    'my_object_page_slug',
    'my_object_page_content',
    'dashicons-admin-site'
);

Example 2

Add a page without a callback function and an icon.

add_object_page(
    'Another Page',
    'Another Menu',
    'manage_options',
    'another_menu_slug'
);

Example 3

Add a page with a custom capability.

function custom_capability_page_content() {
    echo 'This page requires a custom capability to view!';
}

add_object_page(
    'Custom Capability Page',
    'Custom Capability',
    'custom_capability',
    'custom_capability_slug',
    'custom_capability_page_content',
    'dashicons-admin-site'
);