Unable to add submenu under plugin menu in wordpress - php

I'm adding a plugin menu (this gets added correctly) and a submenu under it (this never shows up) like this:
//We'll call the action to add the menu when the plugin is loaded
add_action( "plugins_loaded", "load" );
function load()
{
//Add us to the menu
add_action( "admin_menu", "addToMenu" );
}
function addToMenu()
{
//Main menu
add_plugins_page( "My Plugin", "My Plugin", "administrator", "my-plugin", "handlePlugin" );
//Sub Menu
add_submenu_page( "my-plugin", "test", "test", "administrator", "my-sub-slug", "handleSub" );
}
The above adds the "My Plugin" but not the "test" submenu. What am I doing wrong?

Wordpress does not allow 3rd level menu items by default. Code would be needed to alter how the Wordpress menu works to allow this. The workaround is to put your item not under plugins but at the top level.

Related

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.

How to hide this menus from wordpress admin menu?

How do I hide the menus below from wordpress admin menu except for Admin:
admin.php?page=booki/index.php
admin.php?page=booki/managegcal.php
admin.php?page=booki/userhistory.php
admin.php?page=booki/cancelledbookings.php
admin.php?page=booki/reminders.php
admin.php?page=booki/stats.php
I tried this for first one but not worked:
add_action( 'admin_menu', 'nstrm_remove_admin_submenus', 999 );
function nstrm_remove_admin_submenus() {
remove_submenu_page( 'admin.php', 'booki/index.php' );
}
This menus are made by wordpress Booki plugin:
www.booki.io
I checked the plugin demo and the first admin menu on your list is the main/top admin menu page, so if you remove it the whole menu will disappear.
If you really want to remove the whole menu, this should work:
add_action('admin_menu', 'so_40959455_admin_menu', 999);
function so_40959455_admin_menu() {
remove_menu_page('booki/index.php');
}
If you just want to remove the other submenus, this should work:
add_action('admin_menu', 'so_40959455_admin_menu', 999);
function so_40959455_admin_menu() {
remove_submenu_page('booki/index.php', 'booki/managegcal.php');
remove_submenu_page('booki/index.php', 'booki/userhistory.php');
remove_submenu_page('booki/index.php', 'booki/cancelledbookings.php');
remove_submenu_page('booki/index.php', 'booki/reminders.php');
remove_submenu_page('booki/index.php', 'booki/stats.php');
}

Permission error while developing Wordpress plugin

I'm new to wordpress plugin development & I want to make a plugin. So far I managed to add Menu in Dashboard. Now I want to add a submenu. Menu works when I click on it but submenu doesn't work. It shows this error, You do not have sufficient permissions to access this page. My code is below,
<?php
add_action( 'admin_menu', 'sms_dashmenu');
function sms_dashmenu() {
add_menu_page( 'sms_menu_page', 'SMS Demo','manage_options', __FILE__,'sms_plugin',plugins_url( '/Images/logo.png', __FILE__ ) );
add_submenu_page( __FILE__, 'AdminPanel','Admin', 'manage_options',__FILE__.'/menu1', sms_panel_admin);
}
function sms_plugin(){
echo 'Welcome to the business!';
}
function sms_panel_admin(){
?>
<h2>Admin Panel Design</h2>
<?php
}
?>
Is there something wrong in my code? I need a solution badly. Your help would be appreciated. Tnx.
The fourth parameter in add_menu_page is the menu slug of string type use anything unique you want, and the first parameter of add_submenu_page is the parent slug and that is what you entered for add_menu_page menu slug
add_menu_page( 'sms_menu_page', 'SMS Demo','manage_options', 'smsmenu','sms_plugin',plugins_url( '/Images/logo.png', __FILE__ ) );
add_submenu_page( 'smsmenu', 'AdminPanel','Admin', 'manage_options', 'smsadmin', 'sms_panel_admin');
Check add_menu_page and add_submenu_page

Add custom admin menu to woocommerce

Is it possible to add a new admin menu to the woocommerce admin section in Wordpress?
I've done this with WP E-commerce with my custom plugin so am wandering if the same is true for Woo commerce.
Thanks
Well, if you use something like this:
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page( 'woocommerce', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' );
}
function my_custom_submenu_page_callback() {
echo '<h3>My Custom Submenu Page</h3>';
}
Then you will see a submenu under "Woocommerce" admin menu. For some reason you can´t do same using post_type=shop_order.
"shop_order" is the one you should use to put a submenu under "Woocommerce" one.. but, as i said, don´t know why didn´t work with that particual post_type.
http://codex.wordpress.org/Function_Reference/add_submenu_page
For me the following worked:
add_submenu_page(
'edit.php?post_type=product',
PAGE_TITLE,
MENU_TITLE,
'manage_woocommerce',
'custom_wc_menu'
);
Setting the $parent_slug to edit.php?post_type=product

Wordpress add_submenu_page function not working

I need to create a new menu and a sub menu item for the wordpress administrators. The menu is working fine but the sub menu items ( a duplicate sub menu for the main menu item is being created automatically.. I don't want that ). Both the sub menu items has their href attribute blank. I don't understand what the menu slug is. Please help..
// create custom plugin settings menu
add_action('admin_menu', 'retail_create_menu');
function retail_create_menu() { //create new top-level menu
add_menu_page('Retailers', 'Retailers', 'administrator', __FILE__, 'generate_retailer_list', 'http://localhost/apsm/wp-content/themes/wp-mediamag/functions/retail.ico');
add_submenu_page( __FILE__, 'Add Retailers', 'Add Retailers', 'administrator', 'add_ratilers.php', 'retailer_submenu_callback');
}
The file are inside a folder in my theme...
One simple hack to hide that duplicate menu:
add_submenu_page(
'__FILE__', // parent slug, same as main menu slug
'', // empty page title
'', // empty menu title
'administrator', // same capability as parent
'__FILE__', // same menu slug as parent slug
'generate_retailer_list', // same function as parent
)

Categories