admin管理员组

文章数量:1122832

The add_menu_page documentation says to pass the menu title as the second parameter:

add_menu_page('Page Title', 'Menu Title', ...);

When adding more pages later via add_submenu_page, the main page becomes the first entry in the submenu:


However, I want the first item in the list to have a different name (but still point to the same page), the way Wordpress itself does it:


How could I accomplish that in my plugin?

The add_menu_page documentation says to pass the menu title as the second parameter:

add_menu_page('Page Title', 'Menu Title', ...);

When adding more pages later via add_submenu_page, the main page becomes the first entry in the submenu:


However, I want the first item in the list to have a different name (but still point to the same page), the way Wordpress itself does it:


How could I accomplish that in my plugin?

Share Improve this question edited Nov 2, 2012 at 5:11 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Sep 27, 2012 at 23:13 MegaHitMegaHit 1,5532 gold badges11 silver badges12 bronze badges 0
Add a comment  | 

7 Answers 7

Reset to default 182

You can make the 'slug' for the submenu page equal that of the top level page, and they'll point to the same place:

add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
    add_menu_page('My Page Title', 'My Menu Title', 'manage_options', 'my-menu', 'my_menu_output' );
    add_submenu_page('my-menu', 'Submenu Page Title', 'Whatever You Want', 'manage_options', 'my-menu' );
    add_submenu_page('my-menu', 'Submenu Page Title2', 'Whatever You Want2', 'manage_options', 'my-menu2' );
}

E.g.

Make the slug of the parent menu item and sub-menu same (first one item) as below

function actions_recent_bids_add_admin_page(){

    add_menu_page(
          'Recent Bids',
          'Auction Reports',
          'manage_options',
          'wc-auction-reports',
          'actions_recent_bids_list',
          'dashicons-chart-area',
           56
    );

    add_submenu_page(
          'wc-auction-reports',               // parent slug
          'Recent Bids',                      // page title
          'Recent Bids',                      // menu title
          'manage_options',                   // capability
          'wc-auction-reports',               // slug
          'acutions_customers_spendings_list' // callback
    );


    add_submenu_page(
          'wc-auction-reports',               // parent slug
          'Customer Spending',                // page title
          'Customer Spending',                // menu title
          'manage_options',                   // capability
          'wc-acutions-customers-spendings',  // slug
          'acutions_customers_spendings_list' // callback
    );

    add_submenu_page(
          'wc-auction-reports',          // parent slug
          'Customer Bids',               // page title
          'Customer Bids',               // menu title
          'manage_options',              // capability
          'wc-acutions-customers-bids',  // slug
          'acutions_customers_bids_list' // callback
    );
}

add_action('admin_menu','actions_recent_bids_add_admin_page');

Hi I just spent forever looking for this and the correct way is not listed here. You want to use

remove_submenu_page('parent_slug','parent_slug'); 

to the end of your function

Example :

function posts_sync_menu() {
// Main Menu Item (acts as a holder)
add_menu_page(
    'My Posts',        // Page title
    'My Posts',        // Menu title
    'manage_options',  // Capability
    'my-posts',        // Menu slug needed just for initialisation.
    '',                       // No function/callback needed
    'dashicons-networking',    // Icon
    6                          // Position
);

add_submenu_page(
    'my-posts',         // Parent slug
    'My Posts Listing', // Page title
    'My Posts Listing', // Menu title
    'manage_options',   // Capability
    'my-posts-listing', // Slug
    'my_posts_callback' // Callback to render the page
);

// Remove parent sub-menu entry and take the slugg from the "My Posts Listing" 
remove_submenu_page('my-posts','my-posts'); 

}

Will render this:

So basically they will have same slugg with different menu names just like it was asked.

Update!!! If you are not sure whether the parent menu exists, you can first check it as follows:

function contacts_page_submenu(){
    // Check if menu exist ( if Parent Exist ), we don't need it but he wrote :|  ( i'm editor)
    if(menu_page_url('contacts-parent-menu', false )){
        // Change First Submenu Text
        add_submenu_page(
            'contacts-parent-menu', // Slug Parent required
            'Contacts Settings', 
            'Contacts Settings',
            'manage_options',
            'contacts-parent-menu', //Slug Parent required
            'contacts_submenu_callback' // Give Callback Parent Name
        );
    }
}

add_action('admin_menu', 'contacts_page_submenu');

Tested and works in WordPress 6.

Reference: https://developer.wordpress.org/reference/functions/add_submenu_page/

function content_adder_menu() {
    add_menu_page(
        'Content',     // Page title
        'Content',     // Menu title
        'manage_options',    // Capability required to access
        'content-adder',     // Menu slug
        'content_adder_page' // Callback function to display the page
    );

    // Add a submenu page under the top-level menu
    add_submenu_page(
        'content-adder',    // Parent menu slug
        'Add Content',      // Page title
        'Add Content',      // Menu title
        'manage_options',   // Capability required to access
        'add-content',      // Menu slug
        'add_content_page'  // Callback function to display the page
    );
}

// Callback function for the top-level menu page
function content_adder_page() {
    // Display your top-level menu page content here
    echo '<div class="wrap">';
    echo '<h1>Welcome to Content</h1>';
    // Fetch posts from the 'post' post type
    $args = array(
        'post_type' => 'post', // Adjust post type as needed
        'post_status' => 'publish', // Fetch only published posts
        'posts_per_page' => -1, // Fetch all posts
    );

    $query = new WP_Query($args);

    if ($query->have_posts()) {
        echo '<h2>List of Posts</h2>';
        echo '<ul>';
        while ($query->have_posts()) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
        wp_reset_postdata(); // Restore global post data
    } else {
        echo '<p>No posts found.</p>';
    }
    echo '</div>';
}

// Callback function for the submenu page
function add_content_page() {
    // Display your submenu page content here
    echo '<div class="wrap">';
    echo '<h1>Add Content </h1>';
    // Check if the form is submitted
    if(isset($_POST['submit'])) {
        // Process the form data here
        $url = sanitize_text_field($_POST['url']);
        $content = wp_kses_post($_POST['content']);
        
        // Save or use the submitted data as needed
        // For example, you can save it to a custom post type or display it
        $new_post = array(
            'post_title'    => 'Your Post Title',
            'post_content'  => $content, // The content you want to save
            'post_status'   => 'publish', // Publish the post
            'post_author'   => 1, // Author ID (1 is typically the admin)
            'post_type'     => 'post', // Post type (change if needed)
        );
        $post_id = wp_insert_post($new_post);

        if ($post_id) {
            // The post was successfully inserted
            echo 'Post ID ' . $post_id . ' created.';
        } else {
            // There was an error
            echo 'Error creating the post.';
        }

        echo "<p>URL: $url</p>";
        echo "<div>Content: $content</div>";
    } else {
        // Display the form
        echo '<form method="post">';
        echo '<label for="url">Add URL:</label> <br>';
        echo '<input type="text" name="url" id="url" /><br />';
        echo '<label for="content">Add Content:</label> <br>';
        echo '<textarea name="content" id="content" rows="5" cols="40"></textarea><br />';
        echo '<input type="submit" name="submit" value="Add Content" class="button button-primary" />';
        echo '</form>';
    }
    
    echo '</div>';
}

add_action('admin_menu', 'content_adder_menu');

Simply add this:

$submenu['my-menu'][0][0] = 'My New Menu Title';

For debugging purposes, you can do a print_r($menu) to check the whole WP menu.

add_submenu_page(
        'tut_theme_settings',       // parent slug
        'Front Page Elements 2',    // page title
        'Front Page 2',             // menu title
        'manage_options',           // capability
        'tut_theme_settings2',      // slug
        'theme_front_page_settings' // callback
    ); 

if different name of first sub-menu create same slug of parent and first child and call same function

本文标签: plugin developmentaddmenupage() with different name for first submenu item