Make WordPress Core

Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#14666 closed task (blessed) (fixed)

Create reliable way to remove admin menus and submenus

Reported by: markjaquith's profile 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 14 years ago.

Download all attachments as: .zip

Change History (14)

#1 @scribu
14 years ago

Big +1

#2 @scribu
14 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

#3 @scribu
14 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

#4 @westi
14 years ago

  • Cc westi added
  • Priority changed from normal to high

Yes please :-)

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

#5 @scribu
14 years ago

  • Keywords has-patch added

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

It also removes some repetitive docblock text.

#6 @scribu
14 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_init', 'remove_menu_test');
Version 0, edited 14 years ago by scribu (next)

@scribu
14 years ago

#7 @hakre
14 years ago

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

#8 @mikeschinkel
14 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.

#9 @nacin
14 years ago

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

#10 @WraithKenny
14 years ago

  • Cc Ken@… added

#11 @mikeschinkel
14 years ago

  • Cc mikeschinkel@… added

#12 @scribu
14 years ago

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

#13 @scribu
14 years ago

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

Note: See TracTickets for help on using tickets.