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

如何在WordPress后台添加自定义菜单及子菜单?代码问题求助

Fixing Custom Admin Menu & Submenu Issues in WordPress

Hey there! Let's walk through the problems in your current code and get your custom admin menu working properly, plus add submenus as you need.

Key Issues in Your Original Code

  • Invalid Menu Slug: The 4th parameter of add_menu_page() is supposed to be a unique, string-based slug (like restaurants-admin), not a file path. Using get_template_directory() . '/inc/restaurants.php' here breaks WordPress's admin routing, which is likely causing unexpected behavior.
  • Missing Callback Function Implementation: You specified restaurants_admin_page as the callback, but if this function isn't defined, clicking the menu will lead to a blank page.
  • No Submenu Setup: Your code only adds a main menu—we'll add submenu logic too.

Corrected Full Code

Replace your existing code in functions.php with this:

add_action( 'admin_menu', 'restaurant_menu' );
function restaurant_menu() {
    // Define a unique slug for the main menu to reference later
    $main_menu_slug = 'restaurants-admin';

    // Add the main admin menu
    add_menu_page( 
        'Restaurants Admin Page',       // Page title (shows in browser tab)
        'Restaurants',                  // Sidebar menu label
        'manage_options',               // User capability required to see this menu
        $main_menu_slug,                // Unique menu slug
        'restaurants_admin_page',       // Callback function to render the page
        'dashicons-format-aside',       // Menu icon (uses WordPress dashicons)
        6                               // Position in the sidebar (6 places it below Posts)
    );

    // Add a submenu under the main Restaurants menu
    add_submenu_page(
        $main_menu_slug,                // Parent menu slug (links to our main menu)
        'Restaurant Settings',          // Subpage title
        'Settings',                     // Submenu label
        'manage_options',               // Required capability
        'restaurants-settings',         // Unique submenu slug
        'restaurants_settings_page'     // Callback for the subpage
    );

    // You can add more submenus here by repeating add_submenu_page()
}

// Callback function for the main Restaurants admin page
function restaurants_admin_page() {
    // Always verify user permissions first
    if ( !current_user_can( 'manage_options' ) ) {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }

    // Render your page content
    echo '<div class="wrap">';
    echo '<h1>' . esc_html( get_admin_page_title() ) . '</h1>';
    echo '<p>Welcome to your custom Restaurants management dashboard!</p>';
    // Add your custom forms, tables, or logic here
    echo '</div>';
}

// Callback function for the Settings subpage
function restaurants_settings_page() {
    if ( !current_user_can( 'manage_options' ) ) {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }

    echo '<div class="wrap">';
    echo '<h1>' . esc_html( get_admin_page_title() ) . '</h1>';
    echo '<p>Configure your restaurant settings here.</p>';
    // Add settings fields or options here using WordPress Settings API if needed
    echo '</div>';
}

Additional Tips

  • Clear Cache: After updating the code, clear your browser cache and any WordPress caching plugins—sometimes admin menus don't update immediately.
  • Adjust Capabilities: If you don't want only admins to access the menu, replace manage_options with a lower capability like edit_posts (for editors/authors) or create a custom capability.
  • Organize Code: For better maintainability, you can move the callback functions and menu logic to a separate file (like inc/restaurants.php), then include it in functions.php with require_once get_template_directory() . '/inc/restaurants.php';.

内容的提问来源于stack exchange,提问作者Sergi Khizanishvili

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:06:31