Wordpress

How to add a custom admin menu in WordPress dashboard?

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.

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

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

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

91 TechSquare

Recent Posts

Top 5 Android Phones in India (2024): OnePlus Open, Samsung Galaxy S24 Ultra & More

Top 5 Android Phones in India Top 5 Android Phones in India for 2024: Unmatched…

3 months ago

Chapter 3: Understanding HTML Headings and Paragraphs

https://www.youtube.com/watch?v=QU0Hm7k_hWI Introduction: Previously, in Chapter 2, we discussed html elements and structure. In this chapter,…

4 months ago

Chapter 2: HTML Elements and Structure

https://youtu.be/HihwZMLuXGY Introduction: Welcome back to our HTML course! In the last chapter, we explored the…

5 months ago

Mastering Laravel Eloquent ORM: Easy Guide for Beginners

Introduction Mastering Laravel Eloquent ORM: Easy Guide for Beginners Laravel, one of the most popular…

5 months ago

Chapter 1: Introduction to HTML Basics

https://youtu.be/nPljiJtfBUQ In this chapter We will understand What HTML Is Understanding What HTML Is: Introduction:…

12 months ago

The SOLID principles: how to use them in Laravel to write better code

The SOLID principles are a set of guidelines for writing clean and maintainable code that…

2 years ago

This website uses cookies.