0

When an Administrator for a sub site removes a user from their sub site, I am trying to not only remove the user, but also completely delete them from the entire MU site. I have tried this:

add_action( 'remove_user_from_blog', 'custom_remove_user', 10,4 );
function custom_remove_user( $user_id ) {
     wpmu_delete_user( $user_id );
}

but i am not able to get it to work - it is not actually deleting the user and it fact when creating the user it is not being assigned to the proper sub site.

If I input an actual number of an existing user, the function works properly.

Any other suggestions for how to handle this?

2
  • The users table is the same for the whole WordPress MU. Have you checked your error logs?
    – prosti
    Jan 5 '17 at 17:42
  • @prosti what error logs would i check as I am not seeing any in the generic cPanel error log section.
    – whodeee
    Jan 5 '17 at 18:57
1

The function you are looking for is wpmu_delete_user($user->ID)

I wrote a small plugin to delete users across the whole network by role:

<?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>";
    }
}
0

If I input an actual number of an existing user, the function works properly.

The same user may exist on some other website on your MU farm. Try figuring out the problem from the error log. Is your admin also super admin?

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:    }

This may be the problem. Only super admins can remove the users.

4
  • The user is a newly created user with Editor role only associated with the given site.
    – whodeee
    Jan 5 '17 at 19:09
  • I am trying to add that capability to the Administrator, do you know if that is possible?
    – whodeee
    Jan 5 '17 at 19:55
  • That would be the another question @whodeee. It would be hardly desirable to create another quest with the title. Make the administrator super administrator.
    – prosti
    Jan 5 '17 at 19:59
  • @whodeee, identifying the problem is not a bad thing. Please make sure you tick my answer if possible ✓.
    – prosti
    Jan 5 '17 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.