Using WordPress ‘add_users_page()’ PHP function

The add_users_page() WordPress PHP function is used to add a submenu page to the Users/Profile main menu.

Usage

Here is a simple example of how to use the add_users_page() function:

function register_new_user_page() {
    add_users_page('New User Page', 'New Page', 'manage_options', 'new-user-page', 'display_new_user_page');
}
add_action('admin_menu', 'register_new_user_page');

In the above code, we’re adding a new page titled “New User Page” under the Users menu. The ‘manage_options’ capability means only users with this capability (like administrators) can see this page. ‘new-user-page’ is our unique slug for this page, and ‘display_new_user_page’ would be the function that handles the output of this page (not shown).

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 is ”.
  • $position (int) Optional: The position in the menu order this item should appear. Default is null.

More information

See WordPress Developer Resources: add_users_page

Examples

Register a Custom User Page

function register_custom_user_page() {
    add_users_page('Custom User Page', 'Custom Page', 'edit_posts', 'custom-user-page', 'display_custom_user_page');
}
add_action('admin_menu', 'register_custom_user_page');

In this example, a new page titled “Custom User Page” will be added under the Users menu. The ‘edit_posts’ capability means any user who can edit posts can access this page.

Add a User Profile Settings Page

function register_user_settings_page() {
    add_users_page('Profile Settings', 'Settings', 'read', 'user-settings', 'display_user_settings_page');
}
add_action('admin_menu', 'register_user_settings_page');

Here, we’re creating a new “Profile Settings” page under the Users menu. The ‘read’ capability means any user can see this page.

Add a User Management Page

function register_user_management_page() {
    add_users_page('User Management', 'User Management', 'manage_options', 'user-management', 'display_user_management_page');
}
add_action('admin_menu', 'register_user_management_page');

In this example, a “User Management” page is created under the Users menu. The ‘manage_options’ capability means only administrators can see this page.

Add a User Statistics Page

function register_user_stats_page() {
    add_users_page('User Statistics', 'User Stats', 'manage_options', 'user-stats', 'display_user_stats_page');
}
add_action('admin_menu', 'register_user_stats_page');

This code creates a “User Statistics” page under the Users menu. Again, the ‘manage_options’ capability ensures only administrators can view this page.