top level menu item is also being displayed as sub-menu item - wordpress

I am new to wordpress development. I am trying to create a plugin but my top-level menu item is also being displayed as sub-menu item.below is my code.
<?php
/*
Plugin Name: rooties_main_menu
*/
add_action('admin_menu', 'gpwp_rooties_create_menu');
function gpwp_rooties_create_menu() {
add_menu_page('My Rooties Setting','Rooties Settings', 'manage_options', __FILE__, 'gpwp_rooties_setting_page',plugins_url('/images/wordpress.png',__FILE__) );
add_submenu_page(__FILE__, 'About Rooties Plugin', 'About', 'manage_options',__FILE__.'_about',gpwp_rooties_setting_about_page);
add_submenu_page(__FILE__, 'Today\'s Menu', 'Today\'s Menu', 'manage_options',__FILE__.'_about',gpwp_rooties_setting_menu_form_page);
}
?>
I know this is not the best code as per security prospects. I am trying these at my local system.
As per above code only "About" and "Today's menu" should be displayed as sub-menu but it is also displaying "Rooties Settings" too. Please let me know where i am wrong.

From the Codex: http://codex.wordpress.org/Adding_Administration_Menus#Using_add_submenu_page
For existing WordPress menus, the PHP file that handles the display of
the menu page content. For submenus of a custom top-level menu, a
unique identifier for this sub-menu page.
In situations where a plugin is creating its own top-level menu, the
first submenu will normally have the same link title as the top-level
menu and hence the link will be duplicated. The duplicate link title
can be avoided by calling the add_submenu_page function the first time
with the parent_slug and menu_slug parameters being given the same
value.
There are some code examples here, although they are old -> http://wordpress.org/support/topic/add_menu_page-always-add-an-extra-subpage
Before:
add_menu_page('Section', 'Section', 10, __FILE__, 'section');
add_submenu_page(__FILE__, 'Edit', 'Edit', 10, 'section-edit', 'section_edit');
Fixed:
add_menu_page('Section', 'Section', 10, __FILE__, 'section');
add_submenu_page(__FILE__, 'Edit', 'Edit', 10, __FILE__, 'section_edit');

Related

Wordpress: add_submenu_page() creating error

When I try to create a main menu, with this line
add_menu_page("page title","menu name",10,"test-slug");
Wordpress loads just fine and displays the new menu, but when I try to add a submenu,
add_menu_page("page title","menu name",10,"test-slug");
add_submenu_page("test-slug","sub title","sub menu",10,"test-sub-slug");
only an empty page is displayed, right after half a second. Seems like a php-error, but I can't see the error. Does anyone see what I am doing wrong?
Try the code below in your functions.php file. It should work correctly.
add_menu_page(
'Menu Page Title', // page title
'Menu Menu Text', // menu link text
'manage_options', // capability to access the page
'menu_slug', // page URL slug
'menu_callback_function', // callback function to display the content on options page
'dashicons-format-status', // menu icon
2 // priority
);
add_submenu_page(
'menu_slug', // page URL slug
'Sub Menu Title', // page title
'Sub Menu Text', // menu link text
'manage_options', // capability to access the page
'submenu_slug', // page URL slug
'submenu_callback_function', // callback function to display the content on options page
1 // priority
);
function menu_callback_function(){
echo "This is menu page...";
}
function submenu_callback_function(){
echo "This is submenu page...";
}
Problem solved. add_submenu_page() obviously must be called using a hook. Hence, this way it works and will not cause Wordpress to crash:
function dp_menu_item()
{
add_menu_page("page title","menu name","manage_options","mypage.php");
add_submenu_page("edit.php?post_type=cpt_people","sub title","sub menu","manage_options","mysubpage.php");
}
add_action("admin_menu", "dp_menu_item");

Display custom post type edit and list screens as separate submenu items

I have a custom post type called meeting and I want to add its edit and list screens as separate submenu items under a custom menu item slug meetings_settings.
Here is my current menu setup
add_action('admin_menu', 'wf_meetings_menu');
function wf_meetings_menu() {
add_menu_page('Meetings', 'Meetings', 'manage_options', 'meetings_menu', 'meetings_settings');
add_submenu_page('meetings_menu', 'Meetings Settings', 'Settings', 'manage_options', 'meetings_menu_settings', 'meetings_settings');
// meetings list screen goes here
add_submenu_page('meetings_menu', 'All Meetings', 'All Meetings', 'manage_options', 'meetings_menu_all', 'meetings_all');
// meetings edit screen goes here
add_submenu_page('meetings_menu', 'New Meeting', 'New Meeting', 'manage_options', 'meetings_menu_new', 'meetings_new');
}
From research I see you can add a custom post type as a submenu by setting show_in_menu => 'edit.php?post_type=meeting' on the custom post type, and then setting the draw function for the submenu item to 'edit.php?post_type=meeting'. I'm a little confused with this part, because wouldn't that only include the edit screen for that post type? There are TWO screens for a custom post type: the edit screen and the list screen (plus categories and tags but I don't need those in this case).
How do you differentiate between the two and add both the edit and list screens for a custom post type as submenu items of a regular admin menu item like above?
The first parameter of the add_submenu_page function is the parent slug which in this case is 'edit.php?post_type=meeting' in your scenario you'd want to add a custom link that links to the post type edit screen. so you would add a function in functions.php that would add the link manually
add_action('admin_menu', 'meetings_admin_menu');
function meetings_admin_menu() {
global $submenu;
$new_url = 'post-new.php?post_type=meeting';
$all_url = 'edit.php?post_type=meeting';
$submenu['meetings_menu'][] = array('New Meeting', 'edit_posts', $new_url);
$submenu['meetings_menu'][] = array('All Meetings', 'edit_posts', $all_url);
}
note: the second paramater in the $submenu array() is permissions. change accordingly
and now you only need your add_menu_page function.

Wordpress Admin Top Level Menu Item with no link [duplicate]

This question already has an answer here:
Main menu link also showing on submenu in WP admin panel
(1 answer)
Closed 4 years ago.
I am creating a WordPress plugin that requires a settings are in Wp Admin. The Menu I have created currently appears like so:
LSMWP
LSMWP
Settings
Modules
I am trying to get rid of the first sub menu item so the menu appears like below, I would also like top level item to take you to 'settings':
LSMWP
Settings
Modules
-
Current Code:
add_menu_page( 'LSMWP Settings', 'LSMWP', 'manage_options', 'lsmwp', '', '' );
add_submenu_page( 'lsmwp','LSMWP Settings','Settings','manage_options','lsmwp-settings','create_lsmwp_page','' );
add_submenu_page( 'lsmwp','LSMWP Modules','Modules','manage_options','adny-url','create_lsmwp_page','' );
The best way I have found is to manipulate the sub menu text after registration.
function lsmwp_menu() {
global $submenu;
$submenu['LSMWP'][0][0] = 'Settings';
}
add_action( 'admin_menu', 'lsmwp_menu' );

How to Show WP Plugin Menu for Editor User Roles in WordPress

I'm trying to show my plugin menu / options page for the "Editor" role,
but it's not showing up. How to solve this ? Thank you.
Here's my code :
function jowct_add_plugin_for_editors(){
if (!current_user_can('manage_options')) {
add_menu_page(
'Menu Page Title',
'Menu Title',
'delete_others_pages',
'jowct-wpplugin-menu',
'jowct_menu_option_page',
'dashicons-admin-generic',
'3',
);
}
}
if(is_admin()) {
add_action( 'admin_menu', 'jowct_add_plugin_for_editors' );
}
You can remove the if entirely. (both of them)
You don't need too check for manage_options as you are already checking delete_others_pages
More info https://codex.wordpress.org/Roles_and_Capabilities
function jowct_add_plugin_for_editors(){
add_menu_page(
'Menu Page Title',
'Menu Title',
'delete_others_pages', //this will restrict access
'jowct-wpplugin-menu',
'jowct_menu_option_page',
'dashicons-admin-generic',
'3' // this comma was incorrect syntax
);
}
// action admin_menu will only trigger in the admin, no need for the if.
add_action( 'admin_menu', 'jowct_add_plugin_for_editors' );
Thanks everyone, I just solved this issue.
On this case, I want to show the plugin main menu & sub menu to the administrator role. While the editor role only have access to the main menu.
The key is to set the main menu capability to editor capability such as "moderate_comments", this way both administrator and editor can access this main menu.
For the sub menu, set the capability to "manage_options". This way, only administrator will be able to see this sub menu.
Check out this table :
https://codex.wordpress.org/Roles_and_Capabilities

How do you add a WordPress admin page without adding it to the menu?

I'm building a WordPress plugin and I'd like to have an edit-item page that can't be reached via the submenu (because then the item wouldn't be specified).
This resource (http://codex.wordpress.org/Adding_Administration_Menus) shows how to associate an admin page with a function, but not how to do so without adding it as a menu item.
Can this be done?
Thanks!
Best solution here http://wordpress.org/support/topic/add-backend-page-without-menu-item
use add_submenu_page with parent slug = null
I have finally discovered a way to do this that isn't an ugly hack, doesn't require JS to highlight the desired menu item (and submenu item), and works for regular menus registered by plugins (#Josh's answer only works for custom post types).
Essentially, you just need to register your submenu normally, but then hook into the 'submenu_file' filter to deregister it and optionally also set another submenu item to highlight instead.
function so3902760_wp_admin_menu() {
// Register the parent menu.
add_menu_page(
__( 'Parent title', 'textdomain' )
, __( 'Parent', 'textdomain' )
, 'manage_options'
, 'my_parent_slug'
, 'display_my_menu'
);
// Register the hidden submenu.
add_submenu_page(
'my_parent_slug' // Use the parent slug as usual.
, __( 'Page title', 'textdomain' )
, ''
, 'manage_options'
, 'my_hidden_submenu'
, 'display_my_submenu'
);
}
add_action( 'admin_menu', 'so3902760_wp_admin_menu' );
function so3902760_wp_admin_submenu_filter( $submenu_file ) {
global $plugin_page;
$hidden_submenus = array(
'my_hidden_submenu' => true,
);
// Select another submenu item to highlight (optional).
if ( $plugin_page && isset( $hidden_submenus[ $plugin_page ] ) ) {
$submenu_file = 'submenu_to_highlight';
}
// Hide the submenu.
foreach ( $hidden_submenus as $submenu => $unused ) {
remove_submenu_page( 'my_parent_slug', $submenu );
}
return $submenu_file;
}
add_filter( 'submenu_file', 'so3902760_wp_admin_submenu_filter' );
Yes, this can be done (well, technically, it would be more like registering the whole thing and then removing the menu item later), but It would just be easiest (I think) to check for parameters in the $_GET super-global to indicate that the user wishes to edit a specific item.
For example, you could have a page that lists items to edit, and clicking 'edit' only adds the item's ID to the current URL(query-string).
In the function that displays this page, if ID is defined, give them the page to edit that item.
Otherwise, give them the list view. That's how posts, pages, and other custom post types do it.
add_submenu_page with parent slug = null
OR
add_submenu_page with menu title = null
use this code for creating new page without adding in menu
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page($appname, $appname, 'administrator','custompage', 'custom');
remove_menu_page('custom');
}
function custom()
{
echo "hai";
}
Note: This solution doesn't automatically set the current menu and submenu item. If you want to highlight a particular menu as current when the hidden page is viewed, see my other answer.
From the answers that come before me, you can see that there are many ways to do this. However, there is another way that I think may be the best.
Loading the page differently based on the value of a $_GET query var is one option, but it may not be what some people are looking for.
The suggestions regarding add_submenu_page() are on the right track, but each of the previous suggestions have problems. Setting $menu_title to null doesn't keep the menu item from being displayed, it just makes it so the link doesn't have any text. The link still takes up some room in the menu though, so it looks funny. Setting the $parent_slug to null doesn't have this problem, but I noticed that the page's HTML title doesn't display the $page_title text.
My solution was to set $parent_slug to a fake menu slug, like 'i_dont_exist'. The menu item won't be displayed, and when viewing the admin screen the page title will be filled out properly.
add_submenu_page(
'_doesnt_exist'
,__( 'Page title', 'textdomain' )
,''
,'manage_options'
,'menu_slug'
,'display_my_menu'
);
Yes. It is very possible to make a page cannot be reach via submenu, or even the main menu in the WP admin panel. See the code snippet below.
function myplugin_render_edit_page() {
// Code contains the UI for edit page.
}
/**
* Manage menu items and pages.
*/
function myplugin_register_admin_page() {
global $_registered_pages;
$menu_slug = plugin_basename('myplugin.php');
$hookname = get_plugin_page_hookname($menu_slug,'');
if (!empty($hookname)) {
add_action($hookname, 'myplugin_render_edit_page');
}
$_registered_pages[$hookname] = true;
}
add_action('admin_menu', 'myplugin_register_admin_page');
Hopefully, this will help.
Create sub menu page and parent slug leave it empty like this:
// Create page were you can add new users.
public function add_create_user_menu() {
add_submenu_page(
'',
'Create User',
'Create User',
'manage_options',
'create-user',
array( $this, 'add_create_user_page' )
);
}
You can access it like this:
Add New
I've tried all of the suggestions here but with various issues associated with each.
The WordPress codex for add_submenu_page now gives the correct answer, which is to use options.php as your parent slug. I tried the trick of using a made up name but that gives permissions errors, equally use of null at various locations either causes the menu text to simply be missing (but still clickable) or for the browser title to go missing.
Using options.php worked and I've not seen any issues as a result of its use.
Using add_submenu_page with a parent of NULL definitely works, however if you want to keep the non-linked page associated with a particular menu (say a custom post type menu), you have to use a variation of #Boopathi's answer:
function my_hidden_submenu_page(){
//add the submenu page the usual way
add_submenu_page('edit.php?post_type=custom-type', 'My Page Title', 'My Page Title', 'manage_options', 'my-page-slug', 'my_page_callback');
//then remove it
remove_submenu_page('edit.php?post_type=custom-type','my-page-slug');
}
add_action('admin_menu', 'my_hidden_submenu_page');
It looks as though the two actions would cancel each other out, however remove_submenu_page does not unregister the callback function; it merely removes the link.
This way when someone is viewing your non-linked page, the correct navigation menu (our custom post type menu in this example) will still show as active.
One of the problems I found with merely adding null as the parent slug for a sub menu item is that if you're currently viewing that specific page the submenu itself won't display (at least it didn't for me (along with the page title not showing).
What I did was add an empty span element inside the menu title and use jquery to traverse the parent elements and hide it.
It seems this need is still valid for nowadays version.
I am using WordPress 5.3.2 and I used the following methods to remove the parent named menu from the submenu.
add_action( 'admin_menu', 'submenus' );
function submenus()
{
global $submenu;
$parent_slug = 'parent_slug_name';
// remove parent named menu from submenu because it is always the first one in the submenu array, so the offset is 0 and remove just 1
array_splice( $submenu[$parent_slug], 0, 1 ); // with reindex
// or unset( $submenu[$parent_slug][0] ); // without reindex
}
Hope it helps others who want to achieve the same goal.
About php array_splice()
this method could be found in the source of WP function remove_submenu_page() which is available since WP 3.1.0
Edit / Added
Apart from submenu, parent menu could also be updated in similar way.
For parent menu, the global variable is $menu.
example for reference:
add_action( 'admin_menu', array( $this, 'modify_menu_title' ) );
function modify_menu_title() {
global $menu;
$page = 'some-page-slug';
$new_menu_title = 'New Title Name';
foreach( $menu as $key => $value ) {
if( $menu[$key][2] === $page ) {
$menu[$key][0] = $new_menu_title;
}
}
}
I find you can do it by reusing the insert id, like so:
add_menu_page( 'User AS Packages', 'User AS', 'manage_options', 'myplugin/editaspackages.php', 'UserASPackages', '', 8);
add_menu_page( 'User ARP Packages', 'User ARP', 'manage_options', 'myplugin/editarppackages.php', 'UserARPPackages', '', 8);
add_menu_page( 'AS Packages', 'AS Packages', 'manage_options', 'myplugin/ars-s2.php', 'ARPPackages', '', 8);
The last 3 using position 8 and the last one overrides the two before so the two before do not appear.

Resources