How to add a menu to wordpress dashboard

The add_menu_page function in WordPress is used to add a top-level menu page in the WordPress dashboard. Here’s the basic syntax of the add_menu_page function:

add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = ”, string $icon_url = ”, int $position = null )

Parameters in the function are:

  • $page_title (string): The text to be displayed as the title of the page in the browser window and the page heading.
  • $menu_title (string): The text to be displayed in the menu.
  • $capability (string): The user capability required to access the menu page.
  • $menu_slug (string): A unique slug name for the menu page. This is used in the URL and should be a lowercase alphanumeric string with underscores.
  • $function (callable): The callback function that will be called to render the content of the menu page. This is optional.
  • $icon_url (string): The URL to the icon that will be displayed next to the menu item. This is optional.
  • $position (int): The position of the menu item in the menu.

Here’s an example of how you can define and use this function:

In this example, the add_the_menu_page() function adds a menu page titled “DB Updation” to the WordPress dashboard. The menu item will be accessible to users with the “manage_options” capability. The menu slug is set to 'dbupdate', which can be used to identify and target this menu page.

The 'page_content' is the callback function that will be called to render the content of the menu page. You should define the page_content() function separately and include your desired content and functionality within it.

In this case, the menu page will use the “dashicons-book-alt” icon and appear at the position 99 in the menu hierarchy. You can adjust these values based on your requirements.

Remember to hook the add_the_menu_page() function to the 'admin_menu' action so that it is executed when the WordPress dashboard is being loaded.

Once you have added this code to your theme’s functions.php file or a custom plugin, you should see the “DB Updation” menu item in the WordPress dashboard, and clicking on it will display the content defined in the page_content() function.

Leave a comment

Your email address will not be published. Required fields are marked *