How To Add WordPress Menu on Plugin Activation Tutorial

Reading Time: 8 minutes
156 Views

WordPress plugins are an important component of extending the functionality of your website, and one popular practise is to add a plugin-specific menu to the WordPress admin dashboard. This menu allows users to quickly access numerous plugin functions and options.

We will walk you through the process of installing a WordPress plugin menu on activation, ensuring that your plugin integrates flawlessly with the WordPress admin interface.

By the end of this lesson, you should be able to add a WordPress plugin menu on activation, allowing you to give a user-friendly interface for your plugin’s features and settings.

Read More: How To Remove Page Using Slug in WordPress Tutorial

Let’s get started.

What is WordPress Plugin Menu?

A plugin menu in WordPress is a custom menu or submenu item added to the WordPress admin dashboard by a plugin. Plugin developers can utilise these menus to provide a user interface for customising and utilising their plugins within the WordPress backend.

They provide users with a simple way to view and manage plugin settings and functionalities.

The add_menu_page() function in WordPress is used to create a top-level menu in the admin dashboard. It is commonly used by plugin developers to add their own menu items to the WordPress admin menu.

Syntax

add_menu_page( 
        string $page_title, 
        string $menu_title, 
        string $capability, 
        string $menu_slug, 
        callable $callback = '', 
        string $icon_url = '', 
        int|float $position = null 
 ): string

Explanation,

  • $page_title (string): This parameter specifies the text that appears in the browser’s title bar when the plugin’s menu page is displayed. It should provide a brief, descriptive title for the page.
  • $menu_title (string): This is the text that appears in the actual menu item on the admin dashboard. It should be a concise and clear label for the menu. It’s what users see on the menu.
  • $capability (string): Specifies the user role or capability required to access this menu item. For example, if you set it to ‘manage_options’, only users with the ‘manage_options’ capability (typically administrators) will be able to see and access the menu.
  • $menu_slug (string): This parameter sets the unique identifier, or slug, for the menu item. It’s used as part of the URL for the menu page, so it should be unique to avoid conflicts with other menu items.
  • $callback (callable): This is an optional parameter that specifies the function that will be called when the menu item is clicked. If provided, this function will be responsible for displaying the content of the menu page. If not provided, WordPress will generate a default page with a message.
  • $icon_url (string): An optional parameter that allows you to specify an icon to be displayed next to the menu item. This is typically a URL to the image file for the icon. If not specified, a default WordPress icon will be used.
  • $position (int|float): This parameter determines the position of the menu item in the admin menu. You can use an integer or floating-point number to specify the menu’s position. Lower values place the menu higher in the menu hierarchy.
  • Return Value (string): The function returns the resulting menu’s page hook suffix, which is useful for further customizing or manipulating the menu page.

Read More: Concept of Deactivation Hook in WordPress Tutorial

How To Create Plugin Menu in WordPress?

Let’s create a custom plugin in wordpress where you will see how to add plugin menu,

<?php 

/*
 * Plugin name: Employee Management System
 * description: This is a CRUD Employee Management System
 * Plugin URI: https://example.com/custom-plugin
 * Author: Sample User
 * Auther URI: https://example.com
 * Version: 1.0
 * Requires at least: 6.3.2
 * Requires PHP: 7.4
*/

define("EMS_PLUGIN_PATH", plugin_dir_path(__FILE__));

// Calling action hook to add menu
add_action("admin_menu", "ems_add_admin_menu");

// Add menu 
function ems_add_admin_menu(){

    add_menu_page(
        "Employee Management System", // Page title
        "Employee System",            // Menu title
        "manage_options",             // Access level
        "employee-system",            // Menu slug
        "ems_crud_system",            // Callback function
        "dashicons-admin-home",       // Icon class from "dashicons"
        23                            // Menu position number
    );
}

// Menu handle Callback
function ems_crud_system(){

    include_once(EMS_PLUGIN_PATH."pages/add-employee.php");
}

Go back to your plugin’s list,

Once you activate this plugin, it will create a plugin menu with name Employee System.

When you click on it, it will call the layout of add-employee.php from your custom plugin.

Here, are some access level according to user role in wordpress

Author: publish_posts, edit_posts
Editor: publish_posts, edit_posts, manage_categories
Contributor: edit_posts
Super Admin / Administrator: publish_posts, manage_options, edit_posts

How To Decide Plugin Menu Position in List?

In WordPress, when you create a plugin menu using functions like add_menu_page() or add_submenu_page(), you can specify the position of your menu in the admin menu list.

The position determines where your menu item appears in relation to other menu items.

Default: bottom of menu structure

For the Network Admin menu, the values are different:

Read More: Complete Concept of Plugin Activation Hook in WordPress

Here, you will some more useful links,

That’s it.

We hope this article helped you to learn about How To Add WordPress Plugin Menu on Activation Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more