Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#54026 new enhancement

One admin menu item from different plugins

Reported by: kimjo's profile kimjo Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Administration Keywords: has-patch
Focuses: administration Cc:

Description

Hello.

In this example i have two plugins (which cannot be merged) which should use one admin menu item.

plugin 1:

function test1_admin_menu() {
	
    add_menu_page("Test Plugin", "Test Item", "manage_options", "test-plugin-admin");
	
    add_submenu_page("test-plugin-admin", "", "Subitem for plugin 1", "manage_options", "test1-admin-subitem");
	
    // remove the automatically created submenu
    remove_submenu_page("test-plugin-admin", "test-plugin-admin");
	
}

add_action("admin_menu", "test1_admin_menu");

plugin 2:

function test2_admin_menu() {
	
    add_menu_page("Test Plugin", "Test Item", "manage_options", "test-plugin-admin");
	
    add_submenu_page("test-plugin-admin", "", "Subitem for plugin 2", "manage_options", "test2-admin-subitem");
	
    // remove the automatically created submenu
    remove_submenu_page("test-plugin-admin", "test-plugin-admin");
	
}

add_action("admin_menu", "test2_admin_menu");

This works - and the menu is also shown, if only one plugin is active/enabled.

BUT:
The menu "Test Item" in the admin backend is shown twice. Same ID in HTML, same subitems, same links, same names ... completly copied.

Is there a way to avoid that behaviour ?

I will use a clean admin menu and all my plugins should use one (own) admin menu item as parent.

Attachments (1)

54026.diff (1.2 KB) - added by karpstrucking 3 years ago.

Download all attachments as: .zip

Change History (8)

#1 follow-up: @SergeyBiryukov
3 years ago

  • Keywords close added

Hi there, welcome to WordPress Trac! Thanks for the report.

Is there a way to avoid that behaviour ?

Just removing one of the two add_menu_page() calls (doesn't matter which one) should give you one menu item with two submenus. If that is not feasible for whatever reason, calling remove_menu_page( 'test-plugin-admin' ) after both plugins are loaded should remove the first menu item and keep the second one.

I have just tested both options, and they appear to work. I'm marking the ticket as a close candidate, as it looks like WordPress core works as expected here.

#2 in reply to: ↑ 1 @kimjo
3 years ago

Replying to SergeyBiryukov:

Hi there, welcome to WordPress Trac! Thanks for the report.

Is there a way to avoid that behaviour ?

Just removing one of the two add_menu_page() calls (doesn't matter which one) should give you one menu item with two submenus. If that is not feasible for whatever reason, calling remove_menu_page( 'test-plugin-admin' ) after both plugins are loaded should remove the first menu item and keep the second one.

Thanks for the reply, but that will not work:

I run several plugins and i do not know, which of those are enabled/activated. So each plugin has to create the admin menu item to link the own submenus to it. I have looked around for a function to search for already existing menuitems, but had no success.

I think, that WP (working as a "framework" in this case) should detect and solve this problem automatically.

I have just tested both options, and they appear to work. I'm marking the ticket as a close candidate, as it looks like WordPress core works as expected here.

Sorry, but i think this is not the way, WP should handle those effects.

WP should merge those menu items (in the admin backend) automatically.

#3 @kimjo
3 years ago

  • Keywords close removed

Removing the close tag.
Sorry, i am new to this tracking system and am still learning when an how to set tags etc.

#4 follow-up: @karpstrucking
3 years ago

  • Keywords close added

@kimjo you can wrap your add_menu_page function in an if-statement that checks if the menu item was already added by another plugin:

if ( empty( menu_page_url( 'test-plugin-admin', false ) ) ) {

	add_menu_page("Test Plugin", "Test Item", "manage_options", "test-plugin-admin");
}

#5 in reply to: ↑ 4 @kimjo
3 years ago

  • Keywords has-dev-note added; close removed
  • Version set to 5.8

Replying to karpstrucking:

@kimjo you can wrap your add_menu_page function in an if-statement that checks if the menu item was already added by another plugin:

I am currently using a session flag, but the result is similar to your way.

Nevertheless i think, this should be automatically solved by the WP core and not by hand from every developer.

I our own projects (framework and app development for prod usage) those requests are quite typical for framework updates and enhancements.

@karpstrucking
3 years ago

#6 @karpstrucking
3 years ago

  • Keywords has-patch added

@kimjo patch uploaded

#7 @sabernhardt
3 years ago

  • Keywords has-dev-note removed
  • Type changed from defect (bug) to enhancement
  • Version 5.8 deleted
Note: See TracTickets for help on using tickets.