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.
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.
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:
add_menu_page(): Registers a top-level menu item.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.
For our Knowledge Base plugin, we want a top-level menu labeled "Knowledge Base" and a sub-menu for "Settings."
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 ); }
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.
PHPfunction kb_dashboard_callback() { echo '<div class="wrap">'; echo '<h1>Knowledge Base Dashboard</h1>'; echo '<p>Welcome to your plugin management area.</p>'; echo '</div>'; }
To advance our running project, follow these steps:
admin_menu hook.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.
admin_menu hook. This will fail because the menu global hasn't been initialized yet.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.kb-dashboard) conflicts with another plugin, your menu won't render correctly. Always prefix your slugs (e.g., my-plugin-kb-dashboard).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.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.
Master WordPress security by implementing capability checks. Learn to use current_user_can to restrict admin features and enforce proper access control.
Creating Admin Menus
Plugin Security Best Practices
Composer for Dependencies
Theme Integration Hooks
Managing Assets with Gulp/Webpack
Documentation Standards
Plugin Deployment Strategy
Advanced MVC: Dependency Injection
Handling Large Datasets
Error Handling and Logging