mehrshaddarzi
10/9/2017 - 8:15 PM

Not allow Delete User in Wordpress with condition

Not allow Delete User in Wordpress with condition

add_filter(
  'user_row_actions',
  function($actions, $user_object) {
    $result = new WP_Query(
                array(
                    'author'=> $user_object->ID,
                    'post_type'=>'portfolio',
                    'posts_per_page'=>1,
                )
            );
    if ( count($result->posts) !== 0 ){
      unset($actions['delete']);
    }
    return $actions;
  },
  1,2
);



add_action(
  'load-users.php',
  function() {
    if (isset($_GET['user'])) {
      $user_object = get_userdata($_GET['user']);
          $result = new WP_Query(
                        array(
                            'author'=> $user_object->ID,
                            'post_type'=>'portfolio',
                            'posts_per_page'=>1,
                        )
                    );
        if( count($result->posts) !== 0 ){
        wp_die('This user cannot be deleted');
        }
    }
  }  
);