Approve New Users Before They Can Log In
New sign-ups wait for an administrator to approve them on the Users screen before they can log in, and get an email once approved.
Opens WPIDE on
Code
// Roles that need approval when they sign up (e.g. leave out "customer" so shop buyers are not blocked).
$wpide_approval_roles = array( 'subscriber' );
add_action( 'user_register', function ( $user_id ) use ( $wpide_approval_roles ) {
if ( current_user_can( 'create_users' ) ) {
return; // Created by an administrator (admin screen, REST API or WP-CLI).
}
$user = get_userdata( $user_id );
if ( $user && array_intersect( (array) $user->roles, $wpide_approval_roles ) ) {
update_user_meta( $user_id, 'wpide_pending_approval', 1 );
}
} );
add_filter( 'authenticate', function ( $user ) {
if ( $user instanceof WP_User && get_user_meta( $user->ID, 'wpide_pending_approval', true ) ) {
return new WP_Error( 'wpide_pending_approval', '<strong>Your account is waiting for approval.</strong> You will get an email as soon as it is approved.' );
}
return $user;
}, 50 );
add_filter( 'login_message', function ( $message ) {
if ( isset( $_GET['checkemail'] ) && 'registered' === $_GET['checkemail'] ) {
$message .= '<p class="message">An administrator needs to approve your account before you can log in.</p>';
}
return $message;
} );
add_filter( 'manage_users_columns', function ( $columns ) {
$columns['wpide_approval'] = 'Approval';
return $columns;
} );
add_filter( 'manage_users_custom_column', function ( $output, $column, $user_id ) {
if ( 'wpide_approval' !== $column ) {
return $output;
}
if ( ! get_user_meta( $user_id, 'wpide_pending_approval', true ) ) {
return 'Approved';
}
$url = wp_nonce_url( admin_url( 'admin-post.php?action=wpide_approve_user&user_id=' . (int) $user_id ), 'wpide_approve_user_' . (int) $user_id );
return '<strong style="color:#b32d2e">Pending</strong><br><a href="' . esc_url( $url ) . '">Approve</a>';
}, 10, 3 );
add_action( 'admin_post_wpide_approve_user', function () {
$user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0;
if ( ! $user_id || ! current_user_can( 'edit_user', $user_id ) ) {
wp_die( 'You are not allowed to approve this user.' );
}
check_admin_referer( 'wpide_approve_user_' . $user_id );
delete_user_meta( $user_id, 'wpide_pending_approval' );
$user = get_userdata( $user_id );
if ( $user ) {
wp_mail(
$user->user_email,
sprintf( '[%s] Your account has been approved', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ),
sprintf( "Hello %s,\n\nYour account is approved. You can log in here:\n%s", $user->display_name, wp_login_url() )
);
}
wp_safe_redirect( admin_url( 'users.php' ) );
exit;
} );
Notes
Users who registered before activation are treated as approved.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.