首页 > 解决方案 > How can I change my plugins default page name in WordPress?

问题描述

I'm currently developing my first plugin in WordPress. Since it's a admin only plugin I only need admin panel things.

I've started creating the plugins menu in the left panel:

enter image description here

I've did this by defining the main page and a submenu page:

add_menu_page( 'My Plugin', 'My Plugin', 'edit', 'my-plugin', null, 'none', '58' );
add_submenu_page( 'my-plugin', 'Settings','Settings', 'edit', 'my-plugin-settings', [
    $this,
    'settings_page'
] );

Now I have a submenu with 2 entries. So far so good but I want to change the default submenu page of my plugin to Dashboard instead of the plugins name. I can't find anything in the docs so maybe someone of you knows the trick.

标签: phpwordpressplugins

解决方案


You can't change the name of the main entry in the submenu - add_menu_page only lets you set the page title and the main menu title.

What you can do instead is add a new item to the submenu for the main page. Add a new menu item using add_submenu_page, and use the same slug you used in add_menu_page as both the $parent_slug and $menu_slug parameters, e.g.

$plugin_base_slug = 'my-plugin';

add_menu_page( 'My Plugin', 'My Plugin', 'edit', $plugin_base_slug, null, 'none', '58' );

/* Replace the main menu page in submenu with the menu item "Dashboard" */
add_submenu_page( $plugin_base_slug, 'Dashboard', 'Dashboard', 'edit', $plugin_base_slug, null);

/* Add the rest of your submenu pages */
add_submenu_page( $plugin_base_slug, 'Settings','Settings', 'edit', 'my-plugin-settings', [
    $this,
    'settings_page'
] );

This will replace the main page menu in the submenu (i.e. the one called My Plugin in your example) with the new one you create in add_submenu_page (called Dashboard in this example).

Just make sure you use the same capability and function in your new submenu item so that it does the same thing as the main menu page.


推荐阅读