How to Add a Delete Button to the WordPress Admin Bar

How to Add a Delete Button to the WordPress Admin Bar

I came across this nice little piece of code recently at wpengineer.com. It adds a Delete button to your WordPress Admin Bar when you’re on a single post page. This lets you easily delete a post from your front end – very handy for some sites that end up doing a lot of deleting for one reason or another.

To use this, enter the following code into your functions.php file.  (Appearance > Edit > Theme Functions – functions.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 );

And here’s what it looks like:

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

Note: Technically the code above adds a button that says, “Move to Trash.” If you’d like to change it to say something else like “Delete,” then just change the text inside of the single quotation marks in this section:

'title' => __('Move to Trash'),

For example, change it to …

'title' => __('Delete’),

If you’d like to put a Delete link in the post area itself, check out this post – How to Add a Delete Post Link to Your WordPress Posts.

Photo: Delete sticker icon from BigStock

Tags: