简体   繁体   English

如何在 WordPress 中更改我的插件默认页面名称?

[英]How can I change my plugins default page name in WordPress?

I'm currently developing my first plugin in WordPress.我目前正在 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:我已经开始在左侧面板中创建插件菜单:

在此处输入图像描述

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.现在我有一个包含 2 个条目的子菜单。 So far so good but I want to change the default submenu page of my plugin to Dashboard instead of the plugins name.到目前为止一切顺利,但我想将插件的默认子菜单页面更改为Dashboard而不是插件名称。 I can't find anything in the docs so maybe someone of you knows the trick.我在文档中找不到任何东西,所以也许你们中的某个人知道诀窍。

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.您无法更改子菜单中主条目的名称 - add_menu_page仅允许您设置页面标题和主菜单标题。

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, eg使用add_submenu_page添加一个新菜单项,并使用您在add_menu_page中使用的相同 slug 作为$parent_slug$menu_slug参数,例如

$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 (ie the one called My Plugin in your example) with the new one you create in add_submenu_page (called Dashboard in this example).这将用您在 add_submenu_page 中创建的新菜单(在本例中称为Dashboard )替换子菜单中的主页面菜单(即在您的示例中称为My Plugin的菜单)。

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.只需确保在新的子菜单项中使用相同的功能和 function,以便它与主菜单页面执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM