当用户从MU站点删除时,如何从MU站点删除用户?

时间:2017-01-05 作者:whodeee

当子站点的管理员从其子站点中删除用户时,我不仅要删除该用户,还要从整个MU站点中完全删除该用户。我试过这个:

add_action( \'remove_user_from_blog\', \'custom_remove_user\', 10,4 );
function custom_remove_user( $user_id ) {
     wpmu_delete_user( $user_id );
}
但我无法让它工作-它实际上并没有删除用户,而且在创建用户时,它没有被分配到适当的子站点。

如果我输入了现有用户的实际数量,则该函数工作正常。

对于如何处理这个问题,还有其他建议吗?

2 个回复
SO网友:Adam Rehal

您需要的功能是wpmu_delete_user($user->ID)

我编写了一个小插件,用于按角色删除整个网络中的用户:

<?php
/**
 * Plugin Name: Regbox Delete multisite users
 * Plugin URI: http://www.regbox.se/delete-multisite-users
 * Description: Delete users by role across network
 * Version: 0.1
 * Author: Adam Rehal
 * Author URI: http://www.regbox.se
 * License: GPL2
 */
// Make admin menu item in Users

add_action(\'admin_menu\', \'dmu_submenu_page\');

function dmu_submenu_page() {
    add_submenu_page( \'users.php\', \'Delete multisite users\', \'Delete users\', \'manage_options\', \'delete-multisite-users\', \'dmu_callback\' );
}

function dmu_callback() {
    echo \'<div class="wrap"><div id="icon-tools" class="icon32"></div>\';
    echo \'<h2>Delete multisite users</h2>\';
    echo \'<p>Delete users by role across network. This will delete all users with the selected role from the current site AND from the network</p>\';
    echo \'<p>Users with the same role on other sites will not be removed. You can also use the Count button to count the number of users with a specific role</p>\';
    echo \'</div>\';

    // Get all roles
    global $wp_roles;
    $roles = $wp_roles->get_names();

    // Get all blogs
    $blog_list = get_blog_list( 0, \'all\' ); ?>

    <form method="get" action="users.php?page=delete-multisite-users">
        <input type="hidden" name="page" value="delete-multisite-users">
        <input id="what-to-do" type="hidden" name="action" value="">

        <!--
        Select blog:
        <select name="blog">
        <?php
        foreach ($blog_list as $blog) { ?>
        <option value="<?php echo $blog[\'blog_id\'];?>"><?php echo str_replace(\'/\',\'\',$blog[\'path\']);?></option>
        <?php }//end foreach ?>
        </select><br>
        -->

        Select Role:
        <select name="role">
        <?php foreach($roles as $role_value => $role_name) { ?>
           <option value="<?php echo $role_value;?>"><?php echo $role_name;?></option>
        <?php }//end foreach ?>
        </select>
        <input class="button button-secondary" onClick="javascript:document.getElementById(\'what-to-do\').value = \'delete\';" type="submit" value="Delete">
        <input class="button button-primary" onClick="javascript:document.getElementById(\'what-to-do\').value = \'count\';" type="submit" value="Count">
    </form>
    <?php
    // Needed to make use of wp_delete_user()
    require_once( ABSPATH . \'/wp-admin/includes/user.php\' );
    $data = new WP_User_Query( array( \'role\' => $_GET[\'role\']) );
    $userList = $data->get_results();
    $deleted = 0;
    $counted = 0;
    if($_GET[\'action\'] == \'delete\'){
        if($_GET[\'role\'] != \'administrator\'){
            foreach ($userList as $u) {
                if(wpmu_delete_user( $u->ID )){ $deleted++; }
            }
            echo "<p>" . $deleted . " user(s) deleted</p>";
        }else {
            echo "<p>Admin cannot be deleted</p>";
        }
    }
    if($_GET[\'action\'] == \'count\'){
        foreach ($userList as $u) {
            $counted++;
        }
        echo "<p>" . $counted . " " . $_GET[\'role\'] . " user(s) on current site</p>";
    }
}

SO网友:prosti

如果我输入了现有用户的实际数量,则该函数工作正常。

同一用户可能存在于MU农场的其他网站上。尝试从错误日志中找出问题所在。你的管理员也是超级管理员吗?

File: /wp-admin/includes/ms.php

192: function wpmu_delete_user( $id ) {
...
206:    $_super_admins = get_super_admins();
207:    if ( in_array( $user->user_login, $_super_admins, true ) ) {
208:        return false;
209:    }
这可能就是问题所在。只有超级管理员才能删除用户。