secret
Last active

  • Download Gist
wp-admin-menu-classes.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
<?php
/* wp-admin-menu-classes.php
 
Classes to encapsulate access to admin menu global arrays $menu and $submenu
 
See:
-- http://core.trac.wordpress.org/ticket/12718
-- http://core.trac.wordpress.org/ticket/11517
 
version: 1.05 - Renamed "delete_*" functions to "remove_*" (Oct 6 2010)
version: 1.04 - Another significant update. Attempted to make PHP 4.x compatible (Sept 30 2010)
Added hookname property to both section and item
Renamed property with list of items to be "items" instead of "submenus"
version: 1.03 - Major Rewrite over v1.02. Designed for hopeful inclusion within WordPress v3.1 (Sept 29 2010)
version: 1.02 - Added remove_admin_menu_item(), changed "find-by"=="title" to search on RegEx (Sept 27 2010)
 
Designed for hopeful inclusion within WordPress v3.1
 
Examples of use:
// This example creates one menu in place of Dashboard called "My Menu", adds a few things, and removes all else.
// This example assumes this might only be done for end users, not administrators
 
$dashboard = rename_admin_menu_section('Dashboard','My Menu');
 
// // Alternate approach
// remove_admin_menu_item('index.php'); // Dashboard
// $dashboard = add_admin_menu_section(array(
// 'title' => 'My Menu',
// 'slug' => 'index.php',
// ));
 
remove_admin_menu_item($dashboard,'index.php'); // Dashboard
remove_admin_menu_item($dashboard,'update-core.php'); // Updates
 
$movies = "edit.php?post_type=movie";
copy_admin_menu_item($dashboard,$movies);
$movie_genre = 'edit-tags.php?taxonomy=movie-genre&post_type=movie';
copy_admin_menu_item($dashboard,$movies,$movie_genre);
rename_admin_menu_item($dashboard,$movie_genre,'Movie Genre');
remove_admin_menu_item($movies);
remove_admin_menu_item($movies,$movie_genre);
remove_admin_menu_item($movies,'post-new.php?post_type=movie');
remove_admin_menu_section($movies);
 
$actors = "edit.php?post_type=actor";
copy_admin_menu_item($dashboard,$actors);
remove_admin_menu_item($actors);
remove_admin_menu_item($actors,'post-new.php?post_type=actor');
//remove_admin_menu_section($actors);
 
rename_admin_menu_item($dashboard,'Pages','Other Pages');
 
remove_admin_menu_section('edit.php'); // Posts
remove_admin_menu_section('upload.php'); // Media
remove_admin_menu_section('link-manager.php'); // Links
remove_admin_menu_section('edit-comments.php'); // Comments
remove_admin_menu_section('edit.php?post_type=page'); // Pages
remove_admin_menu_section('plugins.php'); // Plugins
remove_admin_menu_section('themes.php'); // Appearance
remove_admin_menu_section('users.php'); // Users
remove_admin_menu_section('tools.php'); // Tools
remove_admin_menu_section('options-general.php'); // Settings
 
*/
 
 
function add_admin_menu_section($section,$args=array()) {
$new_section = new WP_AdminMenuSection();
return $new_section->initialize($section,$args);
}
 
function add_admin_menu_item($section,$item,$args=array()) {
$section = $temp = get_admin_menu_section($section);
$item = ($section ? $section->add_item($item,$args) : false);
return $item;
}
function remove_admin_menu_item($section,$item=false) {
if (!$item)
$item = $section; // These slugs are often identical
$section = get_admin_menu_section($section);
if ($section)
$section->remove_item($item);
}
function remove_admin_menu_section($section) {
$section = get_admin_menu_section($section);
if ($section)
$section->delete();
}
function rename_admin_menu_section($section,$new_title) {
$section = get_admin_menu_section($section);
if ($section)
$section->set_title($new_title);
return $section;
}
function rename_admin_menu_item($section,$item,$new_title) {
$item = get_admin_menu_item($section,$item);
if ($item)
$item->set_title($new_title);
return $item;
}
function swap_admin_menu_sections($from_section,$to_section) {
$from_section = get_admin_menu_section($from_section);
if ($from_section)
$from_section->swap_with($to_section);
return $section;
}
function get_admin_menu_section($section) {
if (!is_a($section,'WP_AdminMenuSection'))
$section = new WP_AdminMenuSection($section);
return $section;
}
function get_admin_menu_item($section,$item) {
$section = get_admin_menu_section($section);
if (!is_a($item,'WP_AdminMenuItem')) {
$item = $section->find_item($item);
}
return $item;
}
function copy_admin_menu_item($to_section,$from_section,$item=false) {
if (!$item)
$item = $from_section; // These slugs are often identical
$to_section = get_admin_menu_section($to_section);
$item = get_admin_menu_item($from_section,$item);
add_admin_menu_item($to_section,$item);
}
 
class WP_AdminMenuSection {
var $index = 0;
var $items=array();
var $hookname;
function __construct($section=false) {
$this->WP_AdminMenuSection($section);
}
function WP_AdminMenuSection($section=false) {
if ($section) { // $section=false when we need to add one. Static methods would be nicer.
if (is_a($section,'WP_AdminMenuSection')) {
$this->index = &$section->index;
$this->items = &$section->items;
$this->hookname = &$section->hookname;
} else {
global $menu;
global $submenu;
$found = false;
foreach($menu as $index => $section_array) {
if ($section==$section_array[2] || // Find by File/Slug
$section==$section_array[0] || // Find by Title
@preg_match("#^{$section}$#",$section_array[0])) { // Find by Title via RegEx
$found = $index;
} else {
continue;
}
break;
}
$this->index = $found;
if ($found)
$this->refresh();
}
}
}
function initialize($section,$args=array()) {
$args = wp_parse_args($args,array(
'where' => 'bottom' // top or bottom
));
$section = wp_parse_args($section,array(
'title' => false,
'slug' => false,
'page_title' => false,
'icon_src' => false,
'function' => false,
'capability' => 'edit_posts',
));
if (!$section['page_title'])
$section['page_title'] = strip_tags($section['title']);
switch ($args['where']) {
case 'bottom':
$this->hookname = add_menu_page(
$section['page_title'],
$section['title'],
$section['capability'],
$section['slug'],
$section['function'],
$section['icon_src'] );
break;
case 'after': // TODO: Implement this
case 'before': // TODO: Implement this
case 'top': // TODO: Implement this
default:
wp_die("where='{$args['where']}' not yet implemented in WP_AdminMenuSection->initialize().");
}
$this->refresh($section['slug']);
return $this;
}
 
function add_item($item,$args=array()) {
$args = wp_parse_args($args,array(
'where' => 'bottom' // top or bottom
));
global $submenu;
$slug = $this->get_slug();
if (!isset($submenu[$slug]))
$submenu[$slug] = array();
$item_list = &$submenu[$slug];
if (is_a($item,'WP_AdminMenuItem')) {
$item_type = OBJECT;
$item_array = $item->get_array();
} else if ($this->is_item_array($item)) {
$item_type = ARRAY_N;
$item_array = $item;
} else {
$item_type = ARRAY_A;
$item = wp_parse_args($item,array(
'title' => false,
'slug' => false,
'page_title' => false,
'function' => false,
'capability' => 'edit_posts',
));
if (!$item['page_title'])
$item['page_title'] = $item['title'];
$item['hookname'] = add_submenu_page( $slug, $item['page_title'], $item['title'], $item['capability'], $item['slug'], $item['function']);
if ($args['where']!='bottom') // If 'bottom', do nothing more./
$item_array = array_pop($item_list);
}
switch ($args['where']) {
case 'top': // No, array_unshift() won't do this instead.
$last_index = $this->get_last_item_index()+5; // Menus typically go in increments of 5.
$item_list[$last_index] = null; // Create a placeholder at end to allow us to shift them all up
$item_indexes = array_keys($item_list);
$new_item_list = array();
$new_item_list[$item_indexes[0]] = $item_array; // Finally add the item array to the beginning.
for($i = 1; $i<count($item_indexes); $i++) {
$new_item_list[$item_indexes[$i]] = $item_list[$item_indexes[$i-1]];
}
$item_list = $new_item_list;
break;
case 'bottom':
if ($item_type != ARRAY_A) {
// If it's an associative array we need to add it, otherwise it's already part of the menu.
$last_index = $this->get_last_item_index()+5;
$item_list[$last_index] = $item_array;
}
break;
case 'after': // TODO: Implement this
case 'before': // TODO: Implement this
default:
wp_die("where='{$args['where']}' not yet implemented in WP_AdminMenuSection->add_item().");
}
$this->refresh();
return new WP_AdminMenuItem($slug,$last_index);
}
function rename_item($item,$new_title) {
$this->find_item($item)->set_title($new_title);
}
function swap_with($section) {
$with = get_admin_menu_section($section);
global $menu;
$temp = $menu[$this->index];
$menu[$this->index] = $menu[$with->index];
$menu[$with->index] = $temp;
$temp = $this->index;
$this->index = $with->index;
$with->index = $temp;
$temp = $this->items;
$this->items = $with->items;
$with->items = $temp;
}
function delete() {
global $submenu;
unset($submenu[$this->get_slug()]);
global $menu;
unset($menu[$this->index]);
}
function remove_item($item) {
global $submenu;
$index = $this->find_item_index($item);
$item = $this->find_item($index);
unset($submenu[$item->parent_slug][$index]);
unset($this->items[$index]);
 
}
function find_item($item) {
if (!is_a($item,'WP_AdminMenuItem')) {
$index = (is_numeric($item) ? $item : $this->find_item_index($item));
$item = ($index!==false ? $this->items[$index] : false);
}
return $item;
}
function find_item_index($item) {
if (is_a($item,'WP_AdminMenuSection')) {
wp_die('Unexpected: WP_AdminMenuSection passed when WP_AdminMenuItem or WP_AdminMenuItem->slug expected.');
}
if (is_a($item,'WP_AdminMenuItem')) {
$item = $item->get_slug();
}
foreach($this->items as $index => $item_obj) {
if ($item==$item_obj->get_slug()) {
break;
} else if (preg_match("#^{$item}$#",$item_obj->get_title())) {
break;
} else {
$index = false;
}
}
return $index;
}
function get_title() {
return $GLOBALS['menu'][$this->index][0];
}
function set_title($new_title) {
$GLOBALS['menu'][$this->index][0] = $new_title;
}
function get_capability() {
return $GLOBALS['menu'][$this->index][1];
}
function set_capability($new_capability) {
$GLOBALS['menu'][$this->index][1] = $new_capability;
}
function get_file() { // 'slug' & 'file' are synonyms for admin menu
return $GLOBALS['menu'][$this->index][2];
}
function set_file($new_file) {
$GLOBALS['menu'][$this->index][2] = $new_file;
}
function get_slug() { // 'slug' & 'file' are synonyms for admin menu
return $this->get_file();
}
function set_slug($new_slug) {
$this->set_file($new_slug);
}
function get_unused() {
return $GLOBALS['menu'][$this->index][3];
}
function set_unused($new_unused) {
$GLOBALS['menu'][$this->index][3] = $new_unused;
}
function get_class() {
return $GLOBALS['menu'][$this->index][4];
}
function set_class($new_class) {
$GLOBALS['menu'][$this->index][4] = $new_class;
}
function get_id() {
return $GLOBALS['menu'][$this->index][5];
}
function set_id($new_id) {
$GLOBALS['menu'][$this->index][5] = $new_id;
}
function get_icon_src() {
return $GLOBALS['menu'][$this->index][6];
}
function set_icon_src($new_icon_src) {
$GLOBALS['menu'][$this->index][6] = $new_icon_src;
}
function is_item_array($item) {
$is_item_array = true;
if (!is_array($item)) {
$is_item_array = false;
} else {
foreach(array_keys($item) as $key) {
if (!is_numeric($key)) {
$is_item_array = false;
break;
}
}
}
return $is_item_array;
}
function get_last_item_index() {
global $submenu;
$slug = $this->get_slug();
return ($slug ? end(array_keys($submenu[$slug])) : false);
}
function refresh($section=false) { // This in case something external changes the submenu indexes
if (!$section) {
$this->items = $this->get_items($this);
} else {
$this->items = $this->get_items($section);
$this->hookname = get_plugin_page_hookname($section,'');
$this->index = $this->find_index($section);
}
}
function find_index($section) {
global $menu;
$found = false;
foreach($menu as $index => $section_array) {
if ($section==$section_array[2]) {
$found = true;
break;
}
}
return ($found ? $index : false);
}
function get_items($section) {
if (is_a($section,'WP_AdminMenuSection'))
$slug = $section->get_slug();
else if (is_string($section))
$slug = $section;
global $submenu;
$items = array();
if (isset($submenu[$slug])) {
foreach($submenu[$slug] as $index => $item) {
$items[$index] = new WP_AdminMenuItem($slug,$index);
}
}
return $items;
}
}
class WP_AdminMenuItem {
var $index;
var $parent_slug;
var $hookname;
function __construct($parent_slug,$slug) {
$this->WP_AdminMenuItem($parent_slug,$slug);
}
function WP_AdminMenuItem($parent_slug,$slug) {
$this->parent_slug = $parent_slug;
if (is_numeric($slug)) { // $slug is designed for future non-legacy use
$this->index = $slug;
$slug = $this->get_slug();
} else {
$section = new WP_AdminMenuSection($parent_slug);
$item = $section->find_item($slug);
if ($item)
$this->index = $item->index;
}
$this->hookname = get_plugin_page_hookname($slug,$parent_slug);
}
function get_array() { // Only here because WP_AdminMenuSection really needed it.
return $GLOBALS['submenu'][$this->parent_slug][$this->index];
}
function get_title() {
return $GLOBALS['submenu'][$this->parent_slug][$this->index][0];
}
function set_title($new_title) {
$GLOBALS['submenu'][$this->parent_slug][$this->index][0] = $new_title;
}
function get_capability() {
return $GLOBALS['submenu'][$this->parent_slug][$this->index][1];
}
function set_capability($new_capability) {
$GLOBALS['submenu'][$this->parent_slug][$this->index][1] = $new_capability;
}
function get_slug() { // 'slug' & 'file' are synonyms for admin menu
return html_entity_decode($GLOBALS['submenu'][$this->parent_slug][$this->index][2]);
}
function set_slug($new_slug) {
$GLOBALS['submenu'][$this->parent_slug][$this->index][2] = $new_slug;
}
function get_file() { // 'slug' & 'file' are synonyms for admin menu
return $this->get_slug();
}
function set_file($new_file) {
$this->set_slug($new_slug);
}
}

I think there's a slight mistake in line 34...

Instead of 'pause_admin_menu_section();'... shouldn't it be 'pause_admin_menu_section_refresh();' instead??

bedex78 - Good catch. Yes, I made that change to the function name before posting the gist but obviously forgot to test it and find the error. I've updated the gist to use pause_admin_menu_section_refresh() instead. (Sorry for the bother.)

Added a new version. Fixed some bugs, added delete_admin_menu_item().

Added another new version with MAJOR breaking changes. Greatly simplified the API interface with a goal of making it more likely to be incorporated into WordPress v3.1.

BTW, I dubbed this new version 1.03.

This is the version 1.04, and usage examples I was referring to in my email Mike ... read all the trac stuff too. Really hoping this gets in to 3.2. I think it's a waste of time to worry about making it PHP4 compatible at this point, just skip it and build it clean for 3.2.
Eager to know how things went at the dev meeting as I wouldn't want to see #1466(?) as anything more than a stop gap. It's great, but, too limited. Now that I'm able to use this, I'm spoiled. This is right on target.
My only comment is that as you are making sub menu changes you need to make references to parent level items, which pending whether you have previously renamed or not, gets a little awkward. Works, but, seems it could be a little smoother. Seems menu items ( all ) would benefit from being assigned unique ids. Call me crazy, I'm all about numerical ids. They just feel safer and more reliable than slugs or titles, which change with the wind.
In my very novice mind, it seems that this entire menu system would benefit from the goal of eventually having the same capabilities as the new menu system for the front end ... ie perhaps a submenu under tools called "admin menu" that functions very much in the same way as the "menu" under appearances? It should need to be activated in the functions, same as post_thumbs etc.

Some Test Results:

This works:
delete_admin_menu_section('edit-comments.php'); // Comments
This does not:
delete_admin_menu_section('Comments'); // Comments
This however does work:
delete_admin_menu_section('Links'); // Links

I also noticed that in #1466(?) ... sorry, can't remember if that's the right ticket ... the code is referencing the page titles associated with those top level menu "sections". Can this be added here so that if I change "posts" to "news" this change is reflected in the page title of edit.php to avoid confusion.

Works:
$community = rename_admin_menu_section('Posts','Community');
delete_admin_menu_item($community,'edit-tags.php?taxonomy=post_tag'); // Tags

Doesn't Work:
$appearance = "themes.php";
delete_admin_menu_item($appearance,'theme-editor.php'); // Editor

Partially Works:
$appearance = rename_admin_menu_section('Appearance','Site Administration'); ---> works
delete_admin_menu_item($appearance,'theme-editor.php'); ---->doesn't work

Am I missing something Mike?

Really hoping this gets in to 3.2.
I think it's a waste of time to worry about making it PHP4 compatible at this point, just skip it and build it clean for 3.2.

That's now the plan: ignore PHP4 compatibility. It would not be possible without a significant rewrite because of my heavy use of passing objects by reference.

Eager to know how things went at the dev meeting as I wouldn't want to see #1466(?) as anything more than a stop gap.
It's great, but, too limited. Now that I'm able to use this, I'm spoiled. This is right on target.

The dev meeting was postponed for unrelated reasons. Please comment on ticket 12718 (and ticket 14666) adding your support to using these functions if you want to see them included. One option is to include it in v3.1 and do a check to fail if not PHP5. Since there would be no backward compatibility concerns this approach has in general been approved by the core team for other enhancements.

My only comment is that as you are making sub menu changes you need to make references to parent level items, which pending whether you have previously renamed or not, gets a little awkward.
Works, but, seems it could be a little smoother.
Seems menu items ( all ) would benefit from being assigned unique ids.
Call me crazy, I'm all about numerical ids.
They just feel safer and more reliable than slugs or titles, which change with the wind.

The problem with numerical IDs for menu sections and items is the source of the menus is code and not the database there is no where to be the authority for assigning numeric IDs. Numerics work for posts since they are stored in the database as a single arbiter but there's no reasonable way to assign idempotent numeric IDs to the admin menus. So we are stuck with slugs and titles.

That said, you can use object references and they don't care if you rename, i.e.:

$dashboard = get_admin_menu_section('index.php');
$dashboard = rename_admin_menu_section($dashboard,'My Main Admin Menu');
delete_admin_menu_item($dashboard,'index.php');         // Dashboard
delete_admin_menu_item($dashboard,'update-core.php');   // Updates

In my very novice mind, it seems that this entire menu system would benefit from the goal of eventually having the same capabilities as the new menu system for the front end ... ie perhaps a submenu under tools called "admin menu" that functions very much in the same way as the "menu" under appearances?
It should need to be activated in the functions, same as post_thumbs etc.

I hate it when others tell me this so, sorry in advance...., but I think that's plugin territory. WordPress is designed to have a standard admin with replaceable themes. Most people who really need to change the admin menus are site implementors; if end users want to change them they can find a plugin that will give them the option (with these functions, that plugin becomes pretty easy to write.)

This does not (work): delete_admin_menu_section('Comments');

It doesn't work because you are trying to match "Comments" when instead you need to try to match the following (note the values hidden in the HTML) :

Comments <span id="awaiting-mod" class="count-0"><span class="pending-count">0</span></span>

I could have match a substring of the full menu but then you could potentially rename/delete menu you don't want to change. So the solution it to pass it a regular expression like so:

delete_admin_menu_section('Comments .*');

But let me think about it. I might be able to come up with a better answer later. For now, use it as designed.

I also noticed that in #1466(?) ... sorry, can't remember if that's the right ticket ...
the code is referencing the page titles associated with those top level menu "sections".
Can this be added here so that if I change "posts" to "news" this change is reflected in the page title of edit.php to avoid confusion.

Ah, good point. I missed that requirement. I'll see what I can work in but unfortunately I can't do it for a while. Clients are demanding me to produce, sooner than later!

BTW, the ticket to which you refer is #14666.

Doesn't work:
$appearance = "themes.php";
delete_admin_menu_item($appearance,'theme-editor.php'); // Editor
Partially Works: ... delete_admin_menu_item($appearance,'theme-editor.php'); ---->doesn't work

Am I missing something Mike?

heh. I chased my tail around a while on that one too. You will find the answer to why it doesn't work on line 170 of /wp-admin/menu.php (in v3.0.1.) We must thank whoever wrote that code because they decided to set the 'admin_menu' hook priority to 101 which means you need to set your 'admin_menu' hook priority to 102 or greater. Sheesh! I think it's actually really sloppy of the person who wrote that because it causes many people to waste their time trying to track down why code doesn't work when it looks like it "obviously" should. This is yet another thing we should try to get fixed in core, IMO.

Hope this helps.

-Mike

The dev meeting was postponed for unrelated reasons. Please comment on ticket 12718 (and ticket 14666) adding your support to using these functions if you want to see them included. One option is to include it in v3.1 and do a check to fail if not PHP5. Since there would be no backward compatibility concerns this approach has in general been approved by the core team for other enhancements.

I love the optional fail. Will do.

The problem with numerical IDs for menu sections and items is the source of the menus is code and not the database there is no where to be the authority for assigning numeric IDs. Numerics work for posts since they are stored in the database as a single arbiter but there's no reasonable way to assign idempotent numeric IDs to the admin menus. So we are stuck with slugs and titles.

I was envisioning backend content types like nav_menus are now ... admin_nav_menus ... this comment should actually be down there with my mention on similar topic ... object references it is then ;-)

That said, you can use object references and they don't care if you rename, i.e.:

In my very novice mind, it seems that this entire menu system would benefit from the goal of eventually having the same capabilities as the new menu system for the front end ... ie perhaps a submenu under tools called "admin menu" that functions very much in the same way as the "menu" under appearances? It should need to be activated in the functions, same as post_thumbs etc.

I hate it when others tell me this so, sorry in advance...., but I think that's plugin territory. WordPress is designed to have a standard admin with replaceable themes. Most people who really need to change the admin menus are site implementors; if end users want to change them they can find a plugin that will give them the option (with these functions, that plugin becomes pretty easy to write.)

No worries on telling me that ... I'm not easily "miffed" heh. I was thinking in terms of development ease, for instance, if admin menus incorporated the approach that we have in the backend now with nav_menus and did have a place in the database, those numerical id's would be available, then, not only would the work your doing be way easier and "cleaner" per se, a dev only type plugin that would make our lives so much easier for creating custom backends for our clients would be a breeze as well ... that's where my thinking was on that anyway.

I also noticed that in #1466(?) ... sorry, can't remember if that's the right ticket ... the code is referencing the page titles associated with those top level menu "sections". Can this be added here so that if I change "posts" to "news" this change is reflected in the page title of edit.php to avoid confusion.

Ah, good point. I missed that requirement. I'll see what I can work in but unfortunately I can't do it for a while. Clients are demanding me to produce, sooner than later!

Groovy.

BTW, the ticket to which you refer is #14666.

Thanks! I was being too lazy to open the tab again to look.

Doesn't work: $appearance = "themes.php"; delete_admin_menu_item($appearance,'theme-editor.php'); // Editor Partially Works: ... delete_admin_menu_item($appearance,'theme-editor.php'); ---->doesn't work

Am I missing something Mike?

heh. I chased my tail around a while on that one too. You will find the answer to why it doesn't work on line 170 of /wp-admin/menu.php (in v3.0.1.) We must thank whoever wrote that code because they decided to set the 'admin_menu' hook priority to 101 which means you need to set your 'admin_menu' hook priority to 102 or greater. Sheesh! I think it's actually really sloppy of the person who wrote that because it causes many people to waste their time trying to track down why code doesn't work when it looks like it "obviously" should. This is yet another thing we should try to get fixed in core, IMO.

haHA! Well now, I know. Menu item is officially gone thank you.

I'll be heading over to put 2¢ in on the aforementioned topics now ...

oh yeah, there won't be any way to just move one item to another spot will there ... due to the same issue of not having ids huh? I can't imagine any way that could be done ... unless "groups" could be created somehow ... hmmm.

sorry for the nightmare read ... didn't know how to quote properly ...

Hey @10SexyApples:

sorry for the nightmare read ... didn't know how to quote properly ...

To quote lines, start them with a greater than (">"). Here's the: links to how to format (which you can find linked to the top right of the edit box as **Comments are parsed with GitHub Flavored Markdown*)*:

Hope this helps.

-Mike

I love the optional fail. Will do.

Big thanks!

I was envisioning backend content types like nav_menus are now ... admin_nav_menus ... this comment should actually be down there with my mention on similar topic ... object references it is then ;-)
In my very novice mind, it seems that this entire menu system would benefit from the goal of eventually having the same capabilities as the new menu system for the front end ... ie perhaps a submenu under tools called "admin menu" that functions very much in the same way as the "menu" under appearances? It should need to be activated in the functions, same as post_thumbs etc.

Ah, fair point! But, be careful what you wish for. I've been doing a lot of advanced work with the nav_menus and I really, really long for the simplicity of menus that would just be registered. But I digress…

I was thinking in terms of development ease, for instance, if admin menus incorporated the approach that we have in the backend now with nav_menus and did have a place in the database, those numerical id's would be available, then, not only would the work your doing be way easier and "cleaner" per se, a dev only type plugin that would make our lives so much easier for creating custom backends for our clients would be a breeze as well ... that's where my thinking was on that anyway.

Anyway, there could be some value in that but I've learned that large changes don't make their way into WP core, at least not quickly so I'm just really happy with this only slightly evolutionary improvement.

haHA! Well now, I know. Menu item is officially gone thank you.

You've heard that pioneers are the ones with arrows in their backs, right…? ;-)

oh yeah, there won't be any way to just move one item to another spot will there ... due to the same issue of not having ids huh? I can't imagine any way that could be done ... unless "groups" could be created somehow ... hmmm.

Give me a use-case example specific with item names?

I have swap_admin_menu_sections(); would swap_admin_menu_items() be what you'd need?

Hope this helps.

-Mike

To quote lines, start them with a greater than (">"). Here's the: links to how to format (which you can find linked to the top right of the edit box as Comments are parsed with GitHub Flavored Markdown):

Thanks!

I responded via email on this, sorry, but, I had another question anyway, so, here goes. I'm wondering if you foresee there being any issues with hooking into core menus that have been potentially renamed and relocated.
For instance, if a plugin wanted to add an option page, and I had moved and renamed the section they wanted to hook into, could any discrepancies arise, perhaps in how the developer coded it to hook in, that would cause a fail?
Just curious~

if a plugin wanted to add an option page, and I had moved and renamed the section they wanted to hook into, could any discrepancies arise, perhaps in how the developer coded it to hook in, that would cause a fail?

It all depends on what the plugin does. If it renames titles and you are using titles then yes, it could cause problems. But no more than going direct to the $menu or $submenu variables. My functions do nothing to modify the underlying structure nor do they cache their values so they are as safe as you can get but still no silver bullet. Caveat Emptor!

Hope this helps.

-Mike

BTW, on WordPress dev chat they don't want to discuss including these for v3.1 (mostly because they've already blessed the list of tasks and this was not on it.)

oh yeah, there won't be any way to just move one item to another spot will there ... due to the same issue of not having ids huh? I can't imagine any way that could be >>done ... unless "groups" could be created somehow ... hmmm.

Give me a use-case example specific with item names?

I have swap_admin_menu_sections(); would swap_admin_menu_items() be what you'd need?

Hope this helps.

Here's a use case where simply 'moving' is more preferable than 'swapping'....

Let's say I want to move the 'Comments' section below 'Posts' and above 'Media', but I want to maintain the order of the rest of the menu, ie. 'Media', then 'Links', then 'Pages'.

In order to do that, I have to:

  • Swap 'Comments' with 'Media'
  • ..then swap 'Media' with 'Links'
  • ..then swap 'Links' with 'Pages'

And if I were to put 'Comments' above 'Posts' then I would have to add one more line of 'swap' command.

Is it possible to make this more straightforward? Of course, it would be good if we could have this for menu items as well.

Just a thought.

Cheers!

@bedex78 - Of course it would be possible, but it would also be a lot more complicated because of how WordPress currently stores menus as +5 indexes into a set of arrays. The code to make it work correctly is a lot more work than what I've done so far. Also, I'm not sure what the best API interface would be. Maybe I should ask, if you had this functionality what would the function calls look like? What parameters would you pass?

@marcosmlopes - Thanks!

I just found your gist here through your post on the wordpress SE forum. Great stuff! The only thing it's missing is what @bedex78 mentioned - I really want to be able to move things around. It would also be nice if it could handle separators too. You asked before what the function calls would look like for moving a section around. This is what I would have in mind:

<?php
move_admin_menu_section( array(
    'slug'     => 'upload.php' // slug of section to move
    'insert'   => 'before', // or 'after'
    'neighbor' => 'edit.php' // slug of section to insert next to
    'priority' => 10, // similar to wp actions to handle multiple moves
));

Thanks again for your work on this.

@aaemnnosttv Thanks for your kind comments. Right now, I've got my hands full with too many other projects to have time to focus on this, unfortunately. If you want to take a stab a getting this to work I'll be happy to post it as an update.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.