WordPress主题选项页添加子菜单页失败求助
Hey there, let's get that submenu page working under your Theme Options menu! The problem here is with the parent slug you're using in add_submenu_page—you're targeting a custom post type archive URL instead of your actual theme options main menu slug.
Here's the breakdown of the mistake:
Your main theme options menu is registered with the slug theme-options (from the 4th parameter of add_menu_page). But in your submenu code, you used edit.php?post_type=theme-options—that's the URL for a custom post type's list page, not your theme options parent menu.
Corrected Code
First, keep your existing main menu registration (it's perfectly fine):
function add_new_menu_items() { add_menu_page( "Theme Options", "Theme Options", "manage_options", "theme-options", "theme_options_page", "", 99 ); } add_action("admin_menu", "add_new_menu_items");
Now fix the submenu registration by using the correct parent slug (theme-options):
function my_plugin_menu() { add_submenu_page( 'theme-options', // Correct parent slug (matches main menu's slug) 'Registrations', 'Registrations', 'manage_options', 'theme-options-registrations', 'wwpr_page_call' ); } add_action('admin_menu', 'my_plugin_menu');
Key Notes to Ensure It Works:
- The first parameter of
add_submenu_pagemust exactly match the slug of the parent menu (the 4th parameter fromadd_menu_page). - Make sure your
wwpr_page_callcallback function is defined—this is what renders the content of your submenu page. For example:function wwpr_page_call() { // Always check user permissions first if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } // Render your submenu content here echo '<div class="wrap"><h1>Registrations</h1><p>Your submenu page content goes here!</p></div>'; } - Both functions are hooked to
admin_menuwith the default priority (10), which works perfectly—WordPress will handle the parent-child association correctly via the matching slugs.
That should get your Registrations submenu showing up right under Theme Options in the WordPress admin sidebar!
内容的提问来源于stack exchange,提问作者Pityer

