How to add a custom admin menu in WordPress dashboard?

/
/
/
1327 Views

In this article, We will learn to create a custom admin menu in the WordPress dashboard. Firstly we will know what is the admin menu in WordPress, why we will need it in the WordPress dashboard and what can we do with it.

custom admin menu in wordpress

What is a custom admin menu?:

A custom menu represents the custom functionality in WordPress which does not exist in the default WordPress installation. We add a new menu tab in the admin dashboard menu. We can make any name of the menu, URL, icons, etc as per our need.

Why we need a custom admin menu?:

Sometimes we want a custom functionality in WordPress apart from the default WordPress Functionalities in the WordPress admin panel. That’s why we need a custom menu in the WordPress dashboard.

What can we do with a custom admin menu?:

We can do multiple things with the custom menu in the WordPress CMS. Here can be many things to describe the use of an admin menu in WordPress in which some use cases are following as:

  • We can create a crud system.
  • To show data from database.
  • We can show statistics there.
  • To call any APIs.
  • We can display any importent message or indications.
  • We can use it for any kind of other activity to guide users.

To create a custom admin menu we use the admin_menu_page hook in WordPress. By using this WordPress hook we can create the custom admin menu. Syntax of admin_menu_page hook is following as:

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

We can initialize the menu name by using the admin_menu hook in WordPress. We can register the admin menu by the admin_menu hook by connecting to a callback function. See the example below:

add_action("admin_menu", "addMyCustomMenu");

function addMyCustomMenu() {
    add_menu_page( 
      'My Custom Menu', //Title
      'My Custom Menu', //Name
      'manage_options', //capabilities
      'my_custom_menu', //slug
      'my_custom_menu_callback_function', //callback function
      'dashicons-heart', //for icons
      1 //for position

     );     
}

If we have not defined the callback function when clicking on the custom menu will show you a blank area apart from the WordPress admin menu. We can use any name of the callback function as per our convenience. You can use the callback function as the following example.

function my_custom_menu_callback_function()
{
    ?>
    <h1>My Custom Menu Heading</h1>
    <p>This is the custom menu example by 91TechSquare.</p>
    <?php
}

The above code will show a normal HTML text in the admin dashboard by using the callback function. You can see the example in the below image.

custom admin menu in wordpress
Custom admin menu in WordPress

To add multiple custom admin menu at a time:

You can also add multiple admin menus as well. If you want to add multiple custom admin menus in the WordPress dashboard using a single function then you can also do that. You need to use the below code for your reference.

add_action("admin_menu", "addMultipleCustomMenu");

function addMultipleCustomMenu() {
    add_menu_page( 
      'Custom Menu 1', 
      'Custom Menu 1', 
      'manage_options', 
      'custom_menu_1', 
      'custom_menu_1_callback_function', 
      'dashicons-heart',
      2
    );
    add_menu_page( 
      'Custom Menu 2', 
      'Custom Menu 2', 
      'manage_options', 
      'custom_menu_2', 
      'custom_menu_2_callback_function', 
      'dashicons-heart',
      3
    );
}

function custom_menu_1_callback_function()
{
    ?>
    <h1>Custom Menu 1 Heading</h1>
    <p>This is the custom menu 1 example by 91TechSquare.</p>
    <?php
}

function custom_menu_2_callback_function()
{
    ?>
    <h1>Custom Menu 2 Heading</h1>
    <p>This is the custom menu 2 example by 91TechSquare.</p>
    <?php
}

By using the above code you will be able to add multiple custom admin menus at a time. You can see the output of the above code in the below picture.

Multiple custom admin menus
Multiple custom admin menus

I hope, This article helped you to learn how to create an admin menu in WordPress dashboard. If you want to learn more things about WordPress Customization then check our other article for WordPress. Thanks

Leave a Comment

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

This div height required for enabling the sticky sidebar