最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp admin - Stopping user deletion from running on error

programmeradmin0浏览0评论

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
  • 1 Before going any further: seems to me your code accounts only for cases when wp_delete_user is used to generate a partial HTML output. What happens when it is called from a plugin or an ajax call? – Celso Bessa Commented Jan 26, 2021 at 15:10
  • @CelsoBessa - Do you mean if the function 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:21
  • I am guessing that, because delete_user_custom_data() is hooked to delete_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:21
  • I see what you mean. Yes, as stated in my previous comment, his 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:23
  • Depending on the case,maybe you could output the notice HTML and then immediatelly use die or exit, or even wp_die or wp_safe_redirect and wp_delete_user would not continue. – Celso Bessa Commented Jan 26, 2021 at 15:24
 |  Show 1 more comment

1 Answer 1

Reset to default 1

After 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');
发布评论

评论列表(0)

  1. 暂无评论