How to cancel the trash action inside wp_trash_post

I need to restrict the button "EMPTY TRASH" and make it delete only the posts only if the logged in user is the creator.

I tried to use the wp_trash_post and i need to decide in the function if i need to continue to trash or not.

How do i tell WordPress NOT to empty the trash from specific posts?

Topic trash hooks Wordpress

Category Web


It's possible to utilize the pre_delete_post filter to short-circuit the deleting of posts.

add_filter( 'pre_delete_post', 'wpse_224246_pre_delete_post', 10, 3 );
function wpse_224246_pre_delete_post( $delete, $post, $force_delete ) {
  //* Escape early if post isn't already trashed
  if( 'trash' !== $post->post_status ) {
    return $delete;
  }
  //* Go ahead with deleting the post if the current user is the post author 
  if( get_current_user_id() === $post->post_author ) {
    return $delete;
  }
  //* Returning any other value besides null will short circuit the deletion
  return true;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.