WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#14666 closed task (blessed) (fixed)

Create reliable way to remove admin menus and submenus

Reported by: markjaquith Owned by:
Milestone: 3.1 Priority: high
Severity: normal Version:
Component: Administration Keywords: has-patch
Focuses: Cc:

Description

It's time. I'm tired of unsetting global variables. We need a consistent way to remove menus (core or plugin-added).

Suggested:

remove_menu_item() and remove_submenu_item()

I don't want to be dealing with numerical array keys. I want something 100% predictable.

Attachments (1)

14666.diff (9.9 KB) - added by scribu 4 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 scribu4 years ago

Big +1

comment:2 scribu4 years ago

I think remove_menu_item() might be confused with the 3.0 Menus. We should include admin in there. The whole list would look like this:

  • add_admin_menu() - currently add_menu_page()
  • add_admin_submenu() - currently add_menu_page()
  • remove_admin_menu()
  • remove_admin_submenu()

This would be a good opportunity to pass named arguments instead of a numeric list. See #13937

comment:3 scribu4 years ago

  • Summary changed from Create reliable way to remove menus and submenus: remove_menu_item() and remove_submenu_item() to Create reliable way to remove admin menus and submenus

comment:4 westi4 years ago

  • Cc westi added
  • Priority changed from normal to high

Yes please :-)

Naming and arguments should match with the other admin menus functions.

comment:5 scribu4 years ago

  • Keywords has-patch added

14666.diff introduces remove_menu_page() and remove_submenu_page().

It also removes some repetitive docblock text.

comment:6 scribu4 years ago

Usage example:

function remove_menu_test() {
	// Remove the entire Tools menu
	remove_menu_page('tools.php');

	// Remove the Categories submenu from Posts
	remove_submenu_page('edit.php', 'edit-tags.php?taxonomy=category');
}
add_action('admin_menu', 'remove_menu_test');
Last edited 3 years ago by scribu (previous) (diff)

scribu4 years ago

comment:7 hakre4 years ago

Maybe some function to list menu and submenu pages so it's easier to gather what to remove. Just an idea.

comment:8 mikeschinkel4 years ago

Here is almost a full API I've written for managing the admin menus:

https://gist.github.com/792b7aa5b695d1092520

Here are some examples of how they would be used.

<?php

require_once('wp-admin-menu-classes.php');

add_action('admin_menu','my_admin_menu');
function my_admin_menu() {
	swap_admin_menu_sections('Pages','Posts');              // Swap location of Posts Section with Pages Section
	rename_admin_menu_section('Media','Photos & Video');    // Rename Media Section to "Photos & Video"
	delete_admin_menu_section('Links');                     // Get rid of Links Section
	$movie_tags_item_array = get_admin_menu_item_array('Movies','Movie Tags');  // Save off the Movie Tags Menu
	update_admin_menu_section('Movies',array(               // Rename two Movie Menu Items and Delete the Movie Tags Item
		array('rename-item','item'=>'Movies','new_title'=>'List Movies'),
		array('rename-item','item'=>'Add New','new_title'=>'Add Movie'),
		array('delete-item','item'=>'Movie Tags'),
	));
	copy_admin_menu_item('Movies',array('Actors','Add New')); // Copy the 'Add New' over from Actors
	renamed_admin_menu_item('Movies','Add New','Add Actor');  // Rename copied Actor 'Add New' to 'Add Actor
	add_admin_menu_item('Movies',array(                       // (Another way to get a 'Add Actor' Link to a section.)
		'title' => 'Alt Add Actor ',
		'slug' => 'post-new.php?post_type=actor',
	), array(// Add Back the Movie Tags at the end.
		'where'=>'end'
	));
	add_admin_menu_item('Movies',$movie_tags_item_array,array(// Add Back the Movie Tags at the end.
		'where'=>'end'
	));
	delete_admin_menu_section('Actors');                      // Finally just get rid of the actors section
}

I designed the objects to be wrappers around the current global variables as much as possible and not attempt to contain the values themselves so as to be fully 100% compatible with existing code. That design decision made for some slightly unusual usage patterns; i.e. that the list of menu items instances can get out of sync from the $submenu array so the classes automatically refresh themselves until refresh is paused using pause_admin_menu_section_refresh().

Also, the objects themselves don't contain the menu section or item data; those are still contained in the global arrays and as such almost all access to data is made through method calls instead of by reading properties.

I had also planned to implement streamlined alternative wrappers to the existing add_submenu_page() to use $args instead of positional parameters but haven't done it yet. I'd be happy to prioritize doing that if there is interest.

I look forward to your feedback. I'd love to see these used, but I'm also happy to see them discussed or changed significantly if we can collectively make them better.

comment:9 nacin4 years ago

Reelated #12718, where I posted these examples. Meant to post them here.

comment:10 WraithKenny4 years ago

  • Cc Ken@… added

comment:11 mikeschinkel4 years ago

  • Cc mikeschinkel@… added

comment:12 scribu4 years ago

  • Resolution set to fixed
  • Status changed from new to closed

comment:13 scribu4 years ago

(In [15754]) Revert part of [15753]. See [12914]. See #14666

Note: See TracTickets for help on using tickets.