#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)
Change History (14)
#2
@
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
@
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
@
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
@
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
@
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');
#7
@
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
@
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.
Big +1