Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.