Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Created October 9, 2017 20:15
Show Gist options
  • Save mehrshaddarzi/24dc8c6f434e6a81c757fe81dcee49fb to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/24dc8c6f434e6a81c757fe81dcee49fb to your computer and use it in GitHub Desktop.
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');
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment