Clear Pending Users
-
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);
- The topic ‘Clear Pending Users’ is closed to new replies.