Suspend User Accounts
Adds a "Suspend this account" checkbox to the user edit screen that logs the person out everywhere and blocks them from logging in until it is unticked.
Opens WPIDE on
Code
add_action( 'edit_user_profile', function ( $user ) {
if ( ! current_user_can( 'edit_user', $user->ID ) || user_can( $user, 'manage_options' ) ) {
return;
}
wp_nonce_field( 'wpide_suspend_' . $user->ID, 'wpide_suspend_nonce' );
printf(
'<h2>Account status</h2><table class="form-table" role="presentation"><tr><th scope="row">Suspend</th><td><label><input type="checkbox" name="wpide_suspended" value="1" %s> Suspend this account (logs the user out and blocks logging in)</label></td></tr></table>',
checked( (bool) get_user_meta( $user->ID, 'wpide_suspended', true ), true, false )
);
} );
add_action( 'edit_user_profile_update', function ( $user_id ) {
if ( ! isset( $_POST['wpide_suspend_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_suspend_nonce'] ), 'wpide_suspend_' . $user_id ) ) {
return;
}
if ( ! current_user_can( 'edit_user', $user_id ) || user_can( $user_id, 'manage_options' ) ) {
return;
}
if ( ! empty( $_POST['wpide_suspended'] ) ) {
update_user_meta( $user_id, 'wpide_suspended', 1 );
WP_Session_Tokens::get_instance( $user_id )->destroy_all();
} else {
delete_user_meta( $user_id, 'wpide_suspended' );
}
} );
add_filter( 'authenticate', function ( $user ) {
if ( $user instanceof WP_User && get_user_meta( $user->ID, 'wpide_suspended', true ) ) {
return new WP_Error( 'wpide_suspended', '<strong>Error:</strong> This account has been suspended. Please contact the site owner.' );
}
return $user;
}, 60 );
add_filter( 'user_row_actions', function ( $actions, $user ) {
if ( get_user_meta( $user->ID, 'wpide_suspended', true ) ) {
$actions = array( 'wpide_suspended' => '<strong style="color:#b32d2e">Suspended</strong>' ) + $actions;
}
return $actions;
}, 10, 2 );
Notes
Administrators cannot be suspended, and the Users list shows "Suspended" under blocked accounts.
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.