Back to Blog
Lesson 8 of the WordPress Plugin Development: Foundations (PHP & MVC) course
WordPressJune 25, 20263 min read

Creating Admin Menus: A WordPress Plugin Developer’s Guide

Learn how to create a custom admin menu in WordPress to house your plugin's UI. Master the Admin Menu API to build professional dashboards for your users.

WordPressPHPAdmin MenuPlugin DevelopmentMVCplugin-development

Previously in this course, we explored Understanding WordPress Hooks to manage our plugin's logic execution. Now that we have a solid architectural foundation—built upon the principles discussed in Designing for MVC in WordPress—it’s time to give our Knowledge Base plugin a home in the WordPress admin dashboard.

Effective plugin management requires a clean, intuitive admin menu. Users shouldn't have to hunt for your settings or data management tools; by using the WordPress Admin Menu API, you can provide a professional, integrated UI that feels native to the dashboard.

The Admin Menu API from First Principles

In WordPress, adding items to the sidebar is handled via the admin_menu action hook. This hook triggers after the basic admin menu structure is already built, allowing you to inject your own custom items.

There are two primary functions you will use:

  1. add_menu_page(): Registers a top-level menu item.
  2. add_submenu_page(): Registers a sub-menu item under an existing (or custom) top-level menu.

Both functions require a "capability" argument, which ensures that only authorized users can view your plugin's pages. This is a critical security step—never grant access to everyone.

Creating Your Plugin Dashboard: A Worked Example

For our Knowledge Base plugin, we want a top-level menu labeled "Knowledge Base" and a sub-menu for "Settings."

Registering the Menus

Within your plugin's main class (or an admin-specific controller), you should hook into admin_menu. Here is how you implement this:

PHP
#6A9955">// Inside your plugin's Admin class or initialization method
add_action('admin_menu', 'kb_register_admin_menus');

function kb_register_admin_menus() {
    #6A9955">// 1. Register the top-level menu
    add_menu_page(
        'Knowledge Base Settings', #6A9955">// Page Title
        'Knowledge Base',          #6A9955">// Menu Title
        'manage_options',          #6A9955">// Capability
        'kb-dashboard',            #6A9955">// Menu Slug
        'kb_dashboard_callback',   #6A9955">// Callback Function
        'dashicons-book-alt',      #6A9955">// Icon
        56                         #6A9955">// Position
    );

    #6A9955">// 2. Register the sub-menu
    add_submenu_page(
        'kb-dashboard',            #6A9955">// Parent Slug
        'Settings',                #6A9955">// Page Title
        'Settings',                #6A9955">// Menu Title
        'manage_options',          #6A9955">// Capability
        'kb-settings',             #6A9955">// Sub-menu Slug
        'kb_settings_callback'     #6A9955">// Callback Function
    );
}

Creating the Callback Function

The callback function is where you render the HTML for your page. Keep this logic lean; in a proper MVC architecture, this callback should simply trigger a view template or a controller method.

PHP
function kb_dashboard_callback() {
    echo '<div class="wrap">';
    echo '<h1>Knowledge Base Dashboard</h1>';
    echo '<p>Welcome to your plugin management area.</p>';
    echo '</div>';
}

Hands-on Exercise

To advance our running project, follow these steps:

  1. Open your plugin's main class and add an admin_menu hook.
  2. Create a method that registers a top-level menu for the "Knowledge Base" plugin.
  3. Add a sub-menu page named "All Articles."
  4. Verify your changes by refreshing the WordPress admin dashboard. You should see your new menu item appearing at position 56 (near the Settings menu).

Tip: If you're struggling with positioning, check the WordPress Admin API: Mastering the WP_Screen Class for Developers to see how screen options interact with your new page.

Common Pitfalls

  • Wrong Hook: Many beginners try to register menus outside of the admin_menu hook. This will fail because the menu global hasn't been initialized yet.
  • Capability Mismatch: Using read as a capability for a settings page is a security risk. Stick to manage_options for configuration pages to ensure only administrators can access them.
  • Duplicate Slugs: If your menu slug (e.g., kb-dashboard) conflicts with another plugin, your menu won't render correctly. Always prefix your slugs (e.g., my-plugin-kb-dashboard).
  • Forgotten wrap class: Always wrap your callback HTML in a <div class="wrap">. WordPress uses this class to apply correct padding and styling to your admin pages.

Recap

You’ve successfully extended the WordPress dashboard by adding a dedicated area for your plugin. By leveraging add_menu_page and add_submenu_page, you’ve created a professional entry point for users. Remember, your admin menu is the first thing users interact with—keeping the navigation clean and the UI consistent is key to a high-quality plugin.

Up next: We will move beyond simple callbacks and implement the Controller Layer for Admin Pages to keep our plugin code clean and maintainable.

Similar Posts