wordpress 如何在另一个插件菜单下添加新的自定义子菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2240460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:34:03  来源:igfitidea点击:

How to add new custom submenu under another plugins menu

wordpresssubmenu

提问by Tom

There is plugin called Shopp in my WP admin page , this plugin has got top level menu "Shopp" .

在我的 WP 管理页面中有一个名为 Shopp 的插件,这个插件有顶级菜单“Shopp”。

This is the top level menu:

这是顶级菜单:

$menus['main'] = add_menu_page('Shopp', 'Shopp', SHOPP_USERLEVEL, 'shopp-orders', array(&$this,'orders'));

And I've created some plugin which need to add as submenu under "Shopp" top level menu , so it is adding sub menu ( link .....wp-admin/admin.php?page=ach-faq.php ) but when I am clicking on submenu it shows "You do not have sufficient permissions to access this page."

我已经创建了一些需要在“Shopp”顶级菜单下添加子菜单的插件,所以它正在添加子菜单(链接 .....wp-admin/admin.php?page=ach-faq.php )但是当我单击子菜单时,它显示“您没有足够的权限访问此页面”。

Debug result:

调试结果:

Pagenow = admin.php
Parent = shopp-orders
Hookname = shopp_page_ach-faq
Menu = Array
Submenu = Array
Menu nopriv = Array
Submenu nopriv =
Plugin page = ach-faq.php
Registered pages =

My code:

我的代码:

function ach_faq_menu(){
 add_submenu_page('shopp-orders', 'My FAQ Plugin', 'My FAQ Plugin', 8, __FILE__, 'section_1');
}
function section_1(){
 echo 'Text';
}
add_action('admin_menu', 'ach_faq_menu');

How can I fix this ? Please help me !

我怎样才能解决这个问题 ?请帮我 !

回答by Altari

Menu and submenu pages should be called at the same time, and use the same slugs. For example

应该同时调用菜单和子菜单页面,并使用相同的 slug。例如

add_action("admin_menu", "createMyMenus");

function createMyMenus() {
    add_menu_page("My Menu", "My Menu", 0, "my-menu-slug", "myMenuPageFunction");
    add_submenu_page("my-menu-slug", "My Submenu", "My Submenu", 0, "my-submenu-slug", "mySubmenuPageFunction");
}

This would result in a top-level menu "My Menu" with a child of "My Submenu".

这将导致顶级菜单“我的菜单”和“我的子菜单”的子菜单。

The invalid permissions error seems to crop up when you use FILEfor the submenu-slug.

当您将FILE用于子菜单段时,似乎会出现无效权限错误。

回答by Lafif Astahdziq

You can add submenu to existing custom menu (added by other plugin) with little bit tricky

您可以将子菜单添加到现有的自定义菜单(由其他插件添加),有点棘手

you can create menu and then remove the menu itself after you add submenu with same slug and callback.

您可以创建菜单,然后在添加具有相同 slug 和回调的子菜单后删除菜单本身。

Please try this code:

请试试这个代码:

add_action( 'admin_menu', 'add_shopp_submenu' );
function add_shopp_submenu(){
    add_menu_page( __('New Menu', 'your-plugin-text-domain'), __('New Menu', 'your-plugin-text-domain'), 'read', 'ach-faq', 'your_menu_callback');
    add_submenu_page( 'shopp-orders', __('New Menu', 'your-plugin-text-domain'), __('New Menu', 'your-plugin-text-domain'), 'read', 'ach-faq', 'your_menu_callback' );
    remove_menu_page('ach-faq');
}

function your_menu_callback(){
    echo "string";
}

回答by niraj rahi

/*create any function name*/
function process_post() {

 add_menu_page(__('nLr','menu-test'), __('My Plugin','menu-test'), 'manage_options','myplugin', 'myplguin_admin_page', 'dashicons-tickets', 6 );

add_submenu_page('myplugin', __('My Plugin Edit', 'menu-test'), __('My Plugin Edit', 'menu-test'), 'manage_options', 'myplugin_edit', 'myplugin_edit');

 }
/*create callback function for main menu*/

function myplguin_admin_page(){
    echo"welcom to my plugin menu";

}

/* create callback function for submenu */

function myplugin_edit(){
    echo"welcome to submenu";
}
add_action( 'admin_init', 'process_post' );
?>

回答by Dominic Tan

To add it on one of the plugin's parent menu, use add_submenu_page() and set the priority of your add_action() to lower i.e, above 10.

要将其添加到插件的父菜单之一,请使用 add_submenu_page() 并将 add_action() 的优先级设置为较低,即高于 10。

Then in add_submenu_page(), replace 'plugin-parent-menu-slug' with the slug of the parent menu where you want it to add. Example, you want to add it under an admin page with a slug /wp-admin/admin.php?page=plugin-parent-menu-slug.

然后在 add_submenu_page() 中,将 'plugin-parent-menu-slug' 替换为您希望添加的父菜单的 slug。例如,您想将其添加到带有 slug /wp-admin/admin.php?page= plugin-parent-menu-slug的管理页面下。

// set priority to lower i.e. greater than 10
add_action( 'admin_menu', 'my_admin_menu', 20 );

function my_admin_menu() {
    add_submenu_page( 'plugin-parent-menu-slug', 'New Menu', 'New Menu', 'manage_options', 'my-admin-slug', 'my_admin_page' );
}

function my_admin_page(){
    echo "My Admin Page";
}

回答by ovi_mihai

You can by adding the plugin folder name and the home page of the plugin. For example I hooked to the Newsletter plugin by:

您可以通过添加插件文件夹名称和插件的主页。例如,我通过以下方式连接到 Newsletter 插件:

<?php 

add_action('admin_menu', 'add_newsletter_extra_page');

function add_newsletter_extra_page(){
    add_submenu_page( 
        'newsletter/intro.php', 
        'Newsletter', 
        'Subscribers Plus', 
        1, //$capability, 
        'subscribers-plus',
        'newsletter_list_addon' );
}
?> 

回答by ruuter

As Altaristated:

正如阿尔塔里所说:

Menu and submenu pages should be called at the same time

应同时调用菜单和子菜单页

SAME TIME- add_submenu_page must be called from same function as add_menu_page, the function in original plugin. You can not"hack" into another plugins menu from outside of it.

相同的时间- add_submenu_page 必须从与 add_menu_page 相同的函数中调用,原始插件中的函数。您不能从外部“侵入”另一个插件菜单。

回答by user2185466

Go to Appearance -> Menus Then Create Menu under Custom Links, then a new menu created in right side. Now you can just drag that and put under which top level menu you want.

转到外观 -> 菜单,然后在自定义链接下创建菜单,然后在右侧创建一个新菜单。现在,您只需拖动它并将其放在您想要的顶级菜单下即可。