📖 Scenario: You are building a WordPress plugin that adds a new menu page to the WordPress admin dashboard. This menu page will help site administrators access custom settings easily.
🎯 Goal: Build a simple WordPress plugin that adds a custom admin menu page with a title and a callback function that displays a welcome message.
📋 What You'll Learn
Create a function to register the admin menu page
Use the add_menu_page function with specific parameters
Hook the menu registration function to admin_menu action
Create a callback function that outputs a welcome message inside the admin page
💡 Why This Matters
🌍 Real World
Custom admin menu pages help WordPress plugin developers provide easy access to plugin settings and features for site administrators.
💼 Career
Knowing how to add admin menu pages is essential for WordPress plugin development roles and customizing WordPress admin interfaces.
Progress0 / 4 steps
1
Setup the Plugin File and Register the Admin Menu Function
Create a function called myplugin_register_menu that will be used to add the admin menu page. This function should be empty for now.
Wordpress
Hint
Start by defining a PHP function named myplugin_register_menu with empty curly braces.
2
Add the Admin Menu Page Using add_menu_page
Inside the myplugin_register_menu function, add a call to add_menu_page with these exact parameters: page title 'My Plugin Settings', menu title 'My Plugin', capability 'manage_options', menu slug 'myplugin-settings', and callback function myplugin_settings_page.
Wordpress
Hint
Use add_menu_page inside your function with the exact parameters given.
3
Hook the Menu Registration Function to admin_menu Action
Add a line of code that hooks the myplugin_register_menu function to the admin_menu action using add_action.
Wordpress
Hint
Use add_action with the hook name 'admin_menu' and your function name.
4
Create the Callback Function to Display the Admin Page Content
Create a function called myplugin_settings_page that outputs an <h1> heading with the text 'Welcome to My Plugin Settings'.
Wordpress
Hint
Define the callback function that prints the heading inside the admin page.
Practice
(1/5)
1. What is the main purpose of the add_menu_page function in WordPress?
easy
A. To register a new user role
B. To add a submenu item under an existing menu
C. To enqueue scripts and styles in the admin area
D. To add a new top-level menu page to the WordPress admin dashboard
Solution
Step 1: Understand the function purpose
add_menu_page is used to create a new top-level menu in the WordPress admin dashboard.
Step 2: Compare with other options
Adding submenu items uses add_submenu_page, user roles are registered differently, and scripts/styles are enqueued with other functions.
Final Answer:
To add a new top-level menu page to the WordPress admin dashboard -> Option D
Quick Check:
add_menu_page adds top-level menu [OK]
Hint: Remember: add_menu_page = top-level menu [OK]
Common Mistakes:
Confusing add_menu_page with add_submenu_page
Thinking it registers user roles
Mixing it with script enqueue functions
2. Which of the following is the correct way to hook add_menu_page in WordPress?
easy
A. add_action('admin_menu', 'my_menu_function');
B. add_action('init', 'my_menu_function');
C. add_filter('admin_menu', 'my_menu_function');
D. add_action('wp_head', 'my_menu_function');
Solution
Step 1: Identify the correct hook for admin menus
The admin_menu action hook is the correct place to add admin menu pages.
Step 2: Check other hooks
init runs too early, wp_head is for front-end, and add_filter is incorrect for this purpose.
Final Answer:
add_action('admin_menu', 'my_menu_function'); -> Option A
Quick Check:
Use admin_menu hook for admin menus [OK]
Hint: Use admin_menu hook to add admin menus [OK]
Common Mistakes:
Using init hook which is too early
Using add_filter instead of add_action
Hooking into front-end hooks like wp_head
3. What will happen if you run this code in your plugin?
B. Nothing happens because the callback function is missing
C. A new top-level menu named 'My Plugin' appears in admin, showing 'Hello Admin!' when clicked
D. The menu appears but clicking it shows a blank page
Solution
Step 1: Analyze the add_menu_page parameters
The code adds a menu titled 'My Plugin' with capability 'manage_options' and a callback that echoes 'Hello Admin!'.
Step 2: Understand the callback behavior
When the menu is clicked, the callback runs and outputs 'Hello Admin!'. No errors occur because capability and callback are valid.
Final Answer:
A new top-level menu named 'My Plugin' appears in admin, showing 'Hello Admin!' when clicked -> Option C
Quick Check:
Valid add_menu_page with callback shows output [OK]
Hint: Callback echoes content on menu page click [OK]
Common Mistakes:
Assuming callback is missing
Confusing capability with missing parameter
Expecting blank page without callback
4. Identify the error in this code snippet:
add_action('admin_menu', 'register_my_menu');
function register_my_menu() {
add_menu_page('Sample', 'Sample', 'manage_options');
}
medium
A. Callback function is missing but required
B. Missing menu slug parameter in add_menu_page
C. Capability 'manage_options' is invalid
D. Wrong hook used for adding menu page
Solution
Step 1: Check add_menu_page parameters
The function call has only three parameters: page title, menu title, and capability.
Step 2: Verify required parameters
The menu slug parameter is missing, which is required as the fourth argument.
Final Answer:
Missing menu slug parameter in add_menu_page -> Option B
Quick Check:
add_menu_page needs menu slug [OK]
Hint: add_menu_page needs 4+ parameters including slug [OK]
Common Mistakes:
Forgetting the menu slug argument
Assuming callback is mandatory (optional)
Using wrong hook instead of admin_menu
5. You want to add a top-level admin menu page visible only to users who can edit posts, with the menu slug 'custom-editor'. Which code snippet correctly achieves this?