Removing orphan users in WordPress Multisite

admin2025-01-08  6

I've set up WP Multisite and wrote a function to daily delete all the blogs apart from one. Now when you use the wpmu_delete_blog() function the blogs are removed however their users remain, and are not associated to any site. I want to get all those users and remove them straight after deleting the blogs. All the users I create are given the role 'demouser'. How can I do this?

I've set up WP Multisite and wrote a function to daily delete all the blogs apart from one. Now when you use the wpmu_delete_blog() function the blogs are removed however their users remain, and are not associated to any site. I want to get all those users and remove them straight after deleting the blogs. All the users I create are given the role 'demouser'. How can I do this?

Share Improve this question asked Dec 12, 2013 at 22:48 urok93urok93 4,04412 gold badges65 silver badges104 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You'll need to select all available users (see $users below) to loop through each one and decide if it's a demouser; to delete all users with no associated site (see empty($user_blogs) below) you can call wpmu_delete_user() (this will require to load ms.php, if you're loading it within a theme or plugin).

Just add the following snippet right after the part in your code where you're deleting the blogs:

global $wpdb;
$users = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users");

foreach ( $users as $user ) :

    $user_login = $user->user_login; // get login
    $user_id = $user->ID; // get ID

    // check for name
    if ( $user_login == 'demouser' ) :

        $user_blogs = get_blogs_of_user( $user_id ); // get related sites

        // check if empty
        if ( empty($user_blogs) ) :
            require_once ABSPATH . 'wp-admin/includes/ms.php';
            wpmu_delete_user( $user_id ); // delete user
        endif;

    endif;

endforeach;

Please take care of this as it will delete the unassigned users and you'll not be able to restore deleted users!

I know this is an old post, but I used this code to create a couple of WP-CLI commands on a multisite demo and thought it might come in useful.

We have it setup on a crontab to delete sites that are over a day old, plus the code here to clean up any orphaned users. commands are wp mytools clean_users and wp mytools delete.

<?php

class MyTools_WPCLI_Command extends WP_CLI_Command {

    public function clean_users( $args, $assoc_args ) {
        global $wpdb;
        $delete = array();
        $dryrun = false;
        $users  = $wpdb->get_results( "SELECT ID, user_login FROM $wpdb->users" );

        if ( isset( $assoc_args['dry-run'] ) ) {
            $dryrun = true;
        }

        WP_CLI::success( sprintf( 'Found %s users', count( $users ) ) );

        foreach ( $users as $user ) {
            $user_login = $user->user_login;
            $user_id    = $user->ID;

            if ( ! is_super_admin( $user_id ) ) {
                $user_blogs = get_blogs_of_user( $user_id );

                if ( empty( $user_blogs ) ) {
                    $delete[] = $user_id;
                }
            }
        }

        $total = count( $delete );
        if ( ! $total ) {
            WP_CLI::success( 'Nothing to delete!' );
            exit();
        }

        if ( $dryrun ) {
            WP_CLI::success( "[DRYRUN] Would have deleted $total users." );
            exit();
        }

        $progress = \WP_CLI\Utils\make_progress_bar( "Deleting $total Users", $total );
        foreach ( $delete as $user_id ) {
            require_once ABSPATH . 'wp-admin/includes/ms.php';
            wpmu_delete_user( $user_id );
            $progress->tick();
        }
        $progress->finish();
    }
    
    public function delete( $args, $assoc_args ) {

        global $wpdb;
        $blogs  = $wpdb->get_results( "SELECT * FROM {$wpdb->blogs} where blog_id > '1' AND registered < NOW() - INTERVAL 1 DAY" );
        $total  = count( $blogs );
        $dryrun = false;

        if( isset( $assoc_args['dry-run'] ) ) {
            $dryrun = true;
        }

        if( $total < 1 ) {
            WP_CLI::success( 'No sites need deleting.' );
            return false;
        }
        $progress = \WP_CLI\Utils\make_progress_bar( "Deleting $count Sites", $total );
        foreach ( $blogs as $blog ) {
            $parts = explode( '.', $blog->domain );
            if( ! $dryrun ) {
                WP_CLI::runcommand( 'site delete ' . $blog->blog_id . ' --yes --quiet' );
                WP_CLI::runcommand( 'user delete ' . $parts[0] . ' --yes --network --quiet' );
            }
            $progress->tick();
        }
        $progress->finish();

        if( $dryrun ) {
            WP_CLI::success( "[DRYRUN] Would have deleted $total sites." );
        } else {
            WP_CLI::success( "Deleted $total sites." );
        }
    }
}

WP_CLI::add_command( 'mytools', 'MyTools_WPCLI_Command' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266134a1096.html

最新回复(0)