0
0
Wordpressframework~20 mins

Admin menu pages in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Admin Menu Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this WordPress admin menu code?
Consider this code snippet added to a plugin. What will the admin menu show after activation?
Wordpress
<?php
add_action('admin_menu', function() {
  add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', function() {
    echo '<h1>Welcome to My Plugin</h1>';
  }, 'dashicons-admin-generic', 6);
});
AA new top-level menu named 'My Plugin' with a gear icon appears at position 6. Clicking it shows 'Welcome to My Plugin'.
BNo menu appears because the callback function is missing a return statement.
CA submenu under 'Settings' named 'My Plugin' appears instead of a top-level menu.
DThe menu appears but the icon is the default WordPress icon, not a gear.
Attempts:
2 left
💡 Hint
Check the parameters of add_menu_page and the callback function output.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in WordPress admin menu registration?
Identify the code snippet that will cause a syntax error when registering an admin menu page.
Aadd_menu_page('Title', 'Menu', 'manage_options', 'slug', 'my_callback_function', 'dashicons-admin-site', 10);
Badd_menu_page('Title', 'Menu', 'manage_options', 'slug', function() { echo 'Hi' }, 'dashicons-admin-site', 10);
Cadd_menu_page('Title', 'Menu', 'manage_options', 'slug', function() { echo 'Hi'; }, 'dashicons-admin-site', 10);
Dadd_menu_page('Title', 'Menu', 'manage_options', 'slug', null, 'dashicons-admin-site', 10);
Attempts:
2 left
💡 Hint
Look carefully at the anonymous function syntax.
state_output
advanced
2:00remaining
What is the value of $submenu after adding this submenu page?
Given this code snippet, what will be the structure of the global $submenu variable for the 'my-plugin' menu?
Wordpress
<?php
add_action('admin_menu', function() {
  add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page');
  add_submenu_page('my-plugin', 'Sub Page', 'Sub Page', 'manage_options', 'my-plugin-sub', 'my_plugin_sub_page');
});

// After this runs, what is $submenu['my-plugin']?
A
[
  [0 =&gt; ['My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page']]
]
B
[
  [0 =&gt; ['Sub Page', 'manage_options', 'my-plugin-sub', 'my_plugin_sub_page']]
]
C[]
D
[
  [0 =&gt; ['My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page']],
  [1 =&gt; ['Sub Page', 'manage_options', 'my-plugin-sub', 'my_plugin_sub_page']]
]
Attempts:
2 left
💡 Hint
Remember that add_menu_page creates the first submenu item automatically.
🔧 Debug
advanced
2:00remaining
Why does this submenu page not appear under the main menu?
This code adds a submenu page but it does not show under the 'my-plugin' menu. What is the cause?
Wordpress
<?php
add_action('admin_menu', function() {
  add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page');
  add_submenu_page('wrong-slug', 'Sub Page', 'Sub Page', 'manage_options', 'my-plugin-sub', 'my_plugin_sub_page');
});
AThe submenu slug 'wrong-slug' does not match the parent menu slug 'my-plugin', so it is not attached.
BThe user does not have 'manage_options' capability, so the submenu is hidden.
CThe submenu callback function 'my_plugin_sub_page' is missing, so it fails silently.
DThe add_menu_page call is missing the icon parameter, so submenu cannot attach.
Attempts:
2 left
💡 Hint
Check the first parameter of add_submenu_page carefully.
🧠 Conceptual
expert
2:00remaining
Which statement about WordPress admin menu page hooks is true?
Select the correct statement about how WordPress admin menu pages and hooks work.
AThe callback function for add_menu_page runs immediately when the plugin loads, not when the menu is clicked.
BThe 'admin_init' hook is the recommended place to register admin menu pages.
CThe 'admin_menu' action runs before the admin page HTML is generated, allowing menu registration but not output rendering.
DSubmenu pages must be registered before the top-level menu page to appear correctly.
Attempts:
2 left
💡 Hint
Think about when WordPress builds the admin menu and when it renders pages.