php - 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:
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.
解决方案
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.
推荐阅读
- swift - 为什么我的 Realm 数据库中只有一个对象显示在我的 tableView 上?
- r - 使用 dplyr 的 mutate 和 case_when 来测试多列(日期间隔)
- postgresql - 从临时表更新每组选择“最后”行
- sql - 如何删除Oracle中的重复项?
- python - 可以同时写入文件并在不同的程序中读取它吗
- amazon-web-services - 使用 Cloudformation 将网页连接到 rds
- python - SQLAlchemy db.create_all() 错误,未创建数据库
- r - 在线性回归中标记分类变量
- c - 如何使用函数 write 打印 wchar_t
- javascript - 访问对象内部的数组