# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: users, security, moderation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/suspend-user-accounts/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Suspend User Accounts" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
