I am writing the following code to do some cleanup before deleting a user via the admin area
function delete_user_custom_data ($user_id) {
$result = do_some_cleanp($user_id); // The do_some_cleanp() function returns true on success and false on failure
if ($result == false) {
?>
<div class="notice notice-warning is-dismissible">
<p>There was a problem</p>
</div>
<?php
}
}
add_action('delete_user', 'delete_user_custom_data');
What I like to do is prevent the deletion of the user from running if the code in the 'delete_user' action function was not successful. How do I do that?
Thanks.
I am writing the following code to do some cleanup before deleting a user via the admin area
function delete_user_custom_data ($user_id) {
$result = do_some_cleanp($user_id); // The do_some_cleanp() function returns true on success and false on failure
if ($result == false) {
?>
<div class="notice notice-warning is-dismissible">
<p>There was a problem</p>
</div>
<?php
}
}
add_action('delete_user', 'delete_user_custom_data');
What I like to do is prevent the deletion of the user from running if the code in the 'delete_user' action function was not successful. How do I do that?
Thanks.
Share Improve this question asked Jan 26, 2021 at 14:57 GreesoGreeso 2,2247 gold badges33 silver badges57 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1After the discussion with @CelsoBessa in the comments section of the question, I came up with this solution
function display_admin_notices () {
// NOTE: Maybe code should be added here to ensure you are displaying those notices only for certain admin pages
if (isset($_GET['update'])) {
switch ($_GET['update']) {
case NOTICE__ERROR:
$type = 'notice-error';
break;
case NOTICE__WARNING:
$type = 'notice-warning';
break;
case NOTICE__SUCCESS:
$type = 'notice-success';
break;
case NOTICE__INFO:
$type = 'notice-info';
break;
}
?>
<div class="notice is-dismissible">
<p><?php echo stripslashes($_GET['message']); ?></p>
</div>
<?php
}
}
function delete_user_custom_data ($user_id) {
$result = do_some_cleanp($user_id); // The do_some_cleanp() function returns true on success and false on failure
if ($result == false) {
$query_vars = array (
'message' => rawurlencode('There was a problem.'),
'update' => NOTICE__ERROR
);
$redirect_url = add_query_arg(
$query_vars,
network_admin_url('users.php')
);
wp_safe_redirect($redirect_url);
exit;
}
}
// Global constants (within the namespace, but I did not add the namespace to simplify the answer)
const NOTICE__ERROR = 0;
const NOTICE__WARNING = 1;
const NOTICE__SUCCESS = 2;
const NOTICE__INFO = 3;
add_action('admin_notices', 'display_admin_notices');
add_action('delete_user', 'delete_user_custom_data');
delete_user_custom_data()
is called from a plugin or via an ajax call? Or if the user is deleted via an ajax call or via a plugin? In either case, this is a plugin that will NOT be released to the public. It is a controlled environment for a specific business. – Greeso Commented Jan 26, 2021 at 15:21delete_user_custom_data()
is hooked todelete_user
, it will be triggered in cases like when the user deletes his/her profile, when admin delete a user individually and/or bulk deletion, by ajax calls, when other plugins call it behind the hood, etc. But your function as is now outputs a notice in all cases,which might not be seem or make other process fail when they shouldn't. – Celso Bessa Commented Jan 26, 2021 at 15:21die
orexit
, or evenwp_die
orwp_safe_redirect
andwp_delete_user
would not continue. – Celso Bessa Commented Jan 26, 2021 at 15:24