How to Quickly Delete Posts from the WordPress Admin Bar

Home » Snippets » How to Quickly Delete Posts from the WordPress Admin Bar
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

As a WordPress user, have you ever found yourself scrolling through a long list of posts in the admin dashboard, trying to find the one you need to delete? This process can be time-consuming and frustrating, especially if you have a large number of posts. Luckily, there’s a quick and easy solution to this problem: deleting posts directly from the admin bar. In this article, we’ll show you how to add a delete link to the admin bar, allowing you to quickly delete posts from any page on your WordPress site.

					function wpturbo_add_delete_post_link() {
    global $wp_admin_bar, $post;
    if ( !is_admin() ) {
        return;
    }
    if ( !is_object( $post ) || !isset( $post->post_type ) ) {
        return;
    }
    if ( !current_user_can( 'delete_post', $post->ID ) ) {
        return;
    }

    $wp_admin_bar->add_menu( array(
        'id'    => 'delete_post',
        'title' => __( 'Delete Post', 'wpturbo' ),
        'href'  => get_delete_post_link( $post->ID ),
        'parent'=> 'edit',
        'meta'  => array(
            'title' => __( 'Delete Post', 'wpturbo' ),
            'class' => 'wpturbo-delete-post'
        ),
    ) );
}
add_action( 'wp_before_admin_bar_render', 'wpturbo_add_delete_post_link' );
				

In this article, we will explain how to add a custom "Delete Post" link to the WordPress admin bar using a code snippet. This functionality is not provided by default in WordPress and can be extremely useful for website administrators, saving them time and making their workflow more efficient.

The first function in the code snippet is wpturbo_add_delete_post_link(). This function adds a new link to the WordPress toolbar to delete the current post from the front-end of the website.

The global $wp_admin_bar, $post code creates a global variable for the WordPress admin bar and the post currently being viewed. The is_admin() code checks to ensure that the user is in the WordPress admin area before continuing. The is_object() and isset() code checks verify that the current post object is valid. Finally, the current_user_can() code confirms that the user has the permission to delete the current post.

If all of these checks pass, the $wp_admin_bar->add_menu() function adds the "Delete Post" link to the WordPress admin bar. The parameters for this function include an id for the link, a title to be displayed, an href for the link to follow, a parent to control where in the admin bar the link will appear, and additional meta to adjust the title and styling of the link.

When a user clicks on the "Delete Post" link, they will be taken to the WordPress post delete page. This code snippet adds a quick way for website administrators to delete posts from the front-end of their site, without having to navigate through the WordPress admin section.

Register an account to save your snippets or go Pro to get more features.