The admin bar is a featured introduced by WordPress 3.1. It adds useful options such as adding new posts or editing an existing one.
But it do not feature a delete button, so you can’t trash a post without accessing the post lists on the dashboard. This code is used to add a custom Move to Trash option to the WordPress admin bar when a user is viewing a post or a page on their WordPress site.
It adds the “Move to Trash” option only for users who are logged in as super admins and have the WordPress admin bar enabled.
Here is a cool hack
add_action( 'admin_bar_menu', 'crunchify_add_adminbar_trash_menu', 35 ); function crunchify_add_adminbar_trash_menu() { if ( ! is_super_admin() || ! is_admin_bar_showing() ) return; $current_object = get_queried_object(); // check, is the objekt with the value readable if ( ! isset( $current_object->post_author ) ) return; // check, if the user id the same as the author-id if the current post if ( (int) $current_object->post_author !== (int) get_current_user_id() ) return; 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 ) ) { global $wp_admin_bar; $wp_admin_bar->add_menu( array( 'id' => 'delete', 'title' => __( 'Move to Trash' ), 'href' => get_delete_post_link( $current_object->term_id ) ) ); } }
Here’s a breakdown of the code:
add_action( 'admin_bar_menu', 'crunchify_add_adminbar_trash_menu', 35 );
This line of code hooks into the admin_bar_menu
action with a priority of 35, which means that the crunchify_add_adminbar_trash_menu
function will be executed when the admin bar menu is being constructed.
function crunchify_add_adminbar_trash_menu() {
This function is defined and is responsible for adding the “Move to Trash” menu item to the admin bar.
if ( ! is_super_admin() || ! is_admin_bar_showing() ) return;
This line checks whether the current user is a super admin and whether the admin bar is showing. If not, the function returns early, and the “Move to Trash” menu item won’t be added.
$current_object = get_queried_object();
This line gets the currently queried object, which is typically the post or page being viewed.
if ( ! isset( $current_object->post_author ) ) return;
This line checks if the currently queried object has a post author. If not, it returns early.
if ( (int) $current_object->post_author !== (int) get_current_user_id() ) return;
This line checks if the user currently viewing the post or page is the author of that post or page. If the user is not the author, the function returns early.
if ( empty( $current_object ) ) return;
This line checks if the $current_object
is empty, and if it is, the function returns early.
The following block of code checks if the current user has permission to edit the post type of the current object and if the post type supports moving to the trash:
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 ) ) { // Code to add "Move to Trash" menu item to the admin bar }
If these conditions are met, it proceeds to add the “Move to Trash” menu item to the admin bar using the add_menu
method of the global $wp_admin_bar
object.
'id' => 'delete'
: Specifies the ID of the menu item.'title' => __( 'Move to Trash' )
: Sets the title of the menu item.'href' => get_delete_post_link( $current_object->term_id )
: Specifies the link that is executed when the menu item is clicked. It generates a link to move the current object (post or page) to the trash.
Please let us know if you face any issue running this code.