wp-admin-bar-trash

Add trash button to WordPress Admin Bar

Add Custom buttons to WordPress Admin Bar

Since version 3.1 of WordPress you have an Admin Bar with several functions. It's an easy task to add custom buttons to the Admin Bar. I will show you how with an example of a "trash" button. This will be another example how to extend your Admin Bar, as I did it in a previous post, where I added a search field to the Admin Bar.

The following little function needs to be placed in a Plugin or in functions.php of your Theme, if it is for the single view. The title of the button is static, since the WP Object doesn't contain it. Other entries like "Edit post" or similar are contained in the Array and therefore can be used. All can be find in array $post_type_object->labels.

WP Admin Bar with Trash Link

Via add_action I give the function to the hook, which takes care of the output of the content. I set the value 35 as priority, which defines the Reihenfolge in the Admin Bar. Via $current_object we know, where we are on now in our frontend and therefore we can assign the ID of the post and check the rights of each user.
The actual deletion of post, page or custom post type happens with a link, which gets created via the function get_delete_post_link(). In this context there are other function, which makes it easy to create a link for a certain action. All information can be find in wp-includes/link-template.php.

function fb_add_admin_bar_trash_menu() {
  global $wp_admin_bar;

  if ( !is_super_admin() || !is_admin_bar_showing() )
      return;

  $current_object = get_queried_object();

  if ( empty($current_object) )
      return;

  if ( !empty( $current_object->post_type ) && 
     ( $post_type_object = get_post_type_object( $current_object->post_type ) ) && 
     current_user_can( $post_type_object->cap->edit_post, $current_object->ID ) 
  ) {
    $wp_admin_bar->add_menu( 
        array( 'id' => 'delete', 
            'title' => __('Move to Trash'), 
            'href' => get_delete_post_link($current_object->term_id) 
        ) 
    );
  }
}
add_action( 'admin_bar_menu', 'fb_add_admin_bar_trash_menu', 35 );

Alternatively you can also remove entries of your Admin Bar, therefore you have to provide the ID of the entry in the function. In the following example we remove to the link to comment view. You can also find the ID if you lookup the markup in the output, the class of the list element has the ID at the end; Example to the above entry delete: <li class="ab-delete "> or also the comments <li class="ab-comments ">

function fb_admin_bar_render() {
	global $wp_admin_bar;

	$wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'fb_admin_bar_render' );

Comments are closed.

4 comments

  1. tucho235

    if you want add icon, change line #14:

    'title' => __(' img src="/images/trash_24.png" alt="Move to Trash" title="Move to Trash"/' ),

    regards!

  2. shawn

    Does anyone have an idea of how to actually assign a wp_menu to the menu bar?

    I'd like to simply put a button on the admin_menu that says 'nav' and then have a drop down showing a custom wp_nav_menu(). That way I can finally do away with a separate nav bar in my theme and move it up above.

  3. kevin Chard

    Hi shawn,
    This snippet will attach a wordpress menu to the admin bar.
    http://wpsnipp.com/index.php/functions-php/attach-a-navigation-menu-to-the-admin-bar/

  4. Brent Shepherd

    Thanks for the tut.

    It looks like checking `is_admin_bar_showing()` in our function is redundant. The `admin_bar_menu` action is only called after `is_admin_bar_showing()` is called in the `wp_admin_bar_render()` function.