How to add new custom submenu under another plugins menu
Asked Answered
E

7

11

There is plugin called Shopp in my WP admin page , this plugin has got top level menu "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."

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 !

Ecclesiolatry answered 10/2, 2010 at 21:24 Comment(1)
wordpress.stackexchange.com/a/92474Fai
K
24

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

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 FILE for the submenu-slug.

Kerbela answered 17/5, 2010 at 19:42 Comment(3)
why is that not in the documentation. It's not obvious that submenu pages need the same slug.Temptress
@TimJoyce They don't need the same slug. Submenus need the parent slug. If you want a default sub-menu, which I don't see why you wouldn't, you need to use the same slug. You can have an action if you were to click on the top level different from all of the sublevel, but that adds confusion. Standard practice is to have first submenu share the slug and its action will override the action of the parent.Reuven
I followed this article to create a sub-menu under the main menu of an active plugin, so simple. developer.wordpress.org/plugins/administration-menus/sub-menusDigraph
P
4

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.

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.

// 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";
}
Pylorus answered 15/12, 2017 at 7:39 Comment(0)
U
2

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.

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";
}
Ultrared answered 5/8, 2016 at 21:8 Comment(0)
S
1

As Altari stated:

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.

Surcease answered 16/2, 2013 at 19:55 Comment(1)
note that to use add_submenu_page in separate function, wordpress.stackexchange.com/a/92474 helped me and worked.Fai
W
0

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

<?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' );
}
?> 
Waxler answered 29/9, 2011 at 8:12 Comment(0)
D
0
/*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' );
?>
Devour answered 29/6, 2017 at 7:29 Comment(0)
B
-6

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.

Bunnybunow answered 19/3, 2013 at 7:38 Comment(1)
the question is about the administration menu, not the front-end ones.Epiphany

© 2022 - 2024 — McMap. All rights reserved.