#16005 closed defect (bug) (fixed)
When trying to remove an item from the admin bar, a blank spot is added in lieu of it
Reported by: | sorich87 | Owned by: | |
---|---|---|---|
Milestone: | 3.1 | Priority: | normal |
Severity: | normal | Version: | 3.1 |
Component: | Administration | Keywords: | has-patch |
Focuses: | Cc: |
Description
I am trying to remove items from the admin bar (parent or child items) but a blank spot is added in lieu of the removed item. See attached screenshot.
Here is the code I am using:
add_action( 'admin_bar_menu', 'wpss_admin_bar_menu', 100 ); function wpss_admin_bar_menu() { global $wp_admin_bar; $wp_admin_bar->remove_menu( 'new-post' ); $wp_admin_bar->remove_menu( 'my-blogs' ); }
Attachments (7)
Change History (21)
#2
@
14 years ago
- Keywords has-patch 2nd-opinion added
The attached patch fixes the issue, but I am not sure it is the best way to do it.
The error is due to the fact that when remove_node() is called by remove_menu(), it returns an empty menu item (line 200 wp-includes/class-wp-admin-bar.php): $menu_item = null;.
The attached patch prevent render() from returning anything when the menu item is null.
Another way to fix the issue would be to unset the menu item in remove_node() instead of returning null. I tried: unset($menu->{$menu_item_id}) but it worked only for parent items. I am not sure about how to do the same for childs.
#3
@
14 years ago
- Keywords 2nd-opinion removed
Forget my previous comment. The unset works. See latest patch.
#4
@
14 years ago
The code around the patch looks like it can be improved.
First, PHP4 issue with the by-reference variable in the foreach. That needs to be removed (and appears useless based on the patch fuzz).
Scrapping the foreach and doing this should be enough.
if ( isset( $menu->$id ) ) { unset( $menu->id ); return true; } return false;
#6
@
14 years ago
Without the foreach loop, removing child items (e.g. $wp_admin_bar->remove_menu( 'new-post' );) will not work.
#9
@
14 years ago
Patches works well here. To clarify, should that be new-content instead of new-post in the example.
#12
follow-up:
↓ 13
@
14 years ago
- Cc kpdesign added
In a convo with Andrew Nacin on Twitter a couple of days ago, I asked if removing child items was not possible (based on comment 6 to this ticket). He said that the final patch took that into consideration and that child items should be able to be removed.
Using the following code to remove the dashboard links in the admin bar in Multisite:
function remove_admin_bar_dashboard_link() { global $current_user, $wp_admin_bar; if (!current_user_can('edit_posts') && is_admin_bar_showing()) { $wp_admin_bar->remove_menu('dashboard'); } } add_action('admin_bar_menu', 'remove_admin_bar_dashboard_link', 100);
This results in the dashboard link in the My Account menu being removed, but it does not remove the My Sites/[Site Name]/Dashboard links (see attached screenshot).
My understanding of the remove_menu() function is that it looks for the slug for the menu item. Wouldn't the slug for all of the Dashboard links in the admin bar be 'dashboard' and thus removable using the above code?
#13
in reply to:
↑ 12
;
follow-up:
↓ 14
@
14 years ago
Replying to kpdesign:
My understanding of the remove_menu() function is that it looks for the slug for the menu item. Wouldn't the slug for all of the Dashboard links in the admin bar be 'dashboard' and thus removable using the above code?
The ID of these items is not "dashboard" but "blog-{$blog->userblog_id}-d". See /wp-includes/admin-bar.php, line 137.
#14
in reply to:
↑ 13
@
14 years ago
Replying to sorich87:
The ID of these items is not "dashboard" but "blog-{$blog->userblog_id}-d". See /wp-includes/admin-bar.php, line 137.
Are you referring to /wp-includes/admin-bar.php, line 122?
$wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-d', 'title' => __( 'Dashboard' ), 'href' => get_admin_url($blog->userblog_id) ) );
I tried the following code in my function to remove those menu links, based on the id in the above code, without any success - those links still show for a subscriber:
$wp_admin_bar->remove_menu('blog-'.$blog->userblog_id.'-d');
shows the issue