Created
October 9, 2017 20:15
-
-
Save mehrshaddarzi/24dc8c6f434e6a81c757fe81dcee49fb to your computer and use it in GitHub Desktop.
Not allow Delete User in Wordpress with condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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