Adding a custom admin menu page in WordPress using add_menu_page

The add_menu_page function in WordPress is used to add a new top-level menu page to the WordPress admin dashboard. This function can be useful for creating custom menu pages for specific features or settings within a WordPress plugin or theme.

By using add_menu_page, developers can easily organize and structure the admin dashboard to provide a more intuitive and user-friendly experience for website administrators. This function allows for the creation of custom menu pages with unique titles, icons, and access capabilities.

The add_menu_page function is an effective utility for customizing the WordPress admin dashboard and providing easy access to important features or settings within a website.

Parameters accepted by add_menu_page function

  • $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 page and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().
  • $callback (callable, optional, default: ”): The function to be called to output the content for this page.
  • $icon_url (string, optional, default: ”): The URL to the icon to be used for this menu.
  • $position (int/float, optional, default: null): The position in the menu order this item should appear.

The add_menu_page function accepts the above parameters to define the title, menu, capability, slug, callback function, icon URL, and position for the menu page.

Value returned by add_menu_page function

The add_menu_page function returns a string, which is the resulting page’s hook_suffix.

Examples

How to add a menu page in WordPress

Here is an example of how to use the add_menu_page function in WordPress to add a new top-level menu page:

add_action('admin_menu', 'my_custom_menu');

function my_custom_menu() {
 add_menu_page('Custom Menu', 'Custom Menu', 'manage_options', 'custom-menu-slug', 'my_custom_menu_callback', 'dashicons-admin-generic', 6);
}

function my_custom_menu_callback() {
 // Content for the custom menu page goes here
}

This code snippet adds a new top-level menu page called “Custom Menu” to the WordPress admin menu. The page is accessible to users with the ‘manage_options’ capability and has a menu slug of ‘custom-menu-slug’. The callback function my_custom_menu_callback is used to display the content of the menu page.

How to add a submenu page in WordPress

Here is an example of how to use the add_menu_page function in WordPress to add a new submenu page:

add_action('admin_menu', 'my_custom_submenu');

function my_custom_submenu() {
 add_submenu_page('custom-menu-slug', 'Submenu Page', 'Submenu Page', 'manage_options', 'submenu-page-slug', 'my_submenu_page_callback');
}

function my_submenu_page_callback() {
 // Content for the submenu page goes here
}

This code snippet adds a new submenu page called “Submenu Page” to the “Custom Menu” top-level menu page. The submenu page is accessible to users with the ‘manage_options’ capability and has a menu slug of ‘submenu-page-slug’. The callback function my_submenu_page_callback is used to display the content of the submenu page.

How to add a menu page with a custom icon in WordPress

Here is an example of how to use the add_menu_page function in WordPress to add a new top-level menu page with a custom icon:

add_action('admin_menu', 'my_custom_menu_with_icon');

function my_custom_menu_with_icon() {
 add_menu_page('Custom Menu', 'Custom Menu', 'manage_options', 'custom-menu-slug', 'my_custom_menu_callback', 'https://example.com/icon.png', 6);
}

function my_custom_menu_callback() {
 // Content for the custom menu page goes here
}

This code snippet adds a new top-level menu page called “Custom Menu” to the WordPress admin menu with a custom icon specified by the URL ‘https://example.com/icon.png’. The page is accessible to users with the ‘manage_options’ capability and has a menu slug of ‘custom-menu-slug’. The callback function my_custom_menu_callback is used to display the content of the menu page.

Conclusion

In conclusion, the add_menu_page function is an useful utility for adding custom menu pages to WordPress admin dashboard. By utilizing this function, developers can easily create new menu items with custom icons, titles, and capabilities. Additionally, the function allows for the customization of menu positions and the ability to add submenus under existing menu items. With its flexibility and ease of use, add_menu_page is an essential function for any WordPress developer looking to enhance the user experience of their admin dashboard.

Related WordPress Functions