• I was trying to add a function to clear pending users after X days ( which would actually be a nice default functionality ,-)

    the following code should clear pending users if an administrator logs in (using wp_cron is also simply possible). however it is not working, because “$user->roles” only returns the wordpress default user role (like “subscriber”).

    looking into the database I can see that for each user are two “wp_capabilities” entries in the “wp_usermeta” table (while it should be only one entry, right?), for example:

    2397 	112 	wp_capabilities 	a:1:{s:10:"subscriber";b:1;}
    2401 	112 	wp_capabilities 	a:1:{s:7:"pending";b:1;}

    It seems WP_User returns only the one with the lower umeta_id.
    Looking into the TML-Code everything looks fine, it uses set_role( ‘pending’ ) to assign the role.

    Can somebody explain why there are two entries in the database which seems to cause the problem not getting all assigned roles for a user? is this a plugin or core bug?

    function clear_pendingusers($user_login, $curruser) {
    
        if ( in_array('administrator',$curruser->roles) ):
    
            global $wpdb;
    
            $delete_role = 'pending';
            $delete_after_days = 7;
    
            $query = $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE datediff(now(), user_registered) > %d",$delete_after_days);
    
            if ($oldUsers = $wpdb->get_results($query, ARRAY_N)) {
                foreach ($oldUsers as $user_id) {
    
                    $user = new WP_User( $user_id[0] );
                    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
                    	foreach ( $user->roles as $role ) {
                    		if ($role == $delete_role) {
                    		    wp_delete_user($user->ID);
                            }
    
                        }
                    }
                }
            }
    
        endif;
    
    }
    add_action('wp_login', 'clear_pendingusers', 10, 2);

    https://wordpress.org/plugins/theme-my-login/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jeff Farthing

    (@jfarthing84)

    There definately should not be ore than one wp_capabilities meta value for a single user. That’s odd…

    I’m having the same problem with the multiple wp_capabilities for each user.

    When a new user registers it sets one capability to ‘subscriber’ and another to ‘pending’, so the user can actually login without validating his email. Once the user validates through the email link, the double capability persists on the database.

    There’s also two empty ‘default_password_nag’ for each user that has registered.

    I must add, wp_user_level is also duplicated for each new user

    Solved!

    After disabling W3 Total Cache there were no more duplicated metas when registering as a new user.

    I configured W3’s ‘Database Cache’ excluding my login and registration pages and that worked fine.

    (sorry about the reply spam)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Clear Pending Users’ is closed to new replies.