add_menu_page creates an extra submenu item


May 04, 2018 at 05:09
Wordpress

When creating a new main menu item with add_menu_page in the wordpress admin interface, this automatically generates a first submenu item that has the same name as the main menu. Which looks like this: 

To avoid this, it's necessary to set the same slug for the main navigation and the first (!) submenu item: 

add_menu_page( 
	'Ophi Mainmenu',
	'Ophi',
	'manage_options',
	'ophi-mainmenu',
	'some-function',
	'dashicons-media-code',
	10 );

add_submenu_page(
	'ophi-submenu-1',
	'Ophi Submenu1',
	'Submenu1',
	'manage_options',
	'ophi-mainmenu',
	array($this, 'submenuOneAction')
);

add_submenu_page(
	'ophi-submenu-2',
	'Ophi Submenu2',
	'Submenu2',
	'manage_options',
	'ophi-submenu-two',
	array($this, 'submenuTwoAction')
);

Via the duplicate slag ophi-mainmenu the unwanted submenu is replaced by submenu1 and now looks like this: 


Tags: Admin wordpress 4.9.5


Hinterlasse einen Kommentar:
Search