You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress主题选项页添加子菜单页失败求助

Fixing WordPress Theme Options Submenu Page Issue

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_page must exactly match the slug of the parent menu (the 4th parameter from add_menu_page).
  • Make sure your wwpr_page_call callback 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_menu with 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:45:24