Halloween Sale 15% off Ends in See the plans
PHP

Let Members Deactivate Their Own Account

Adds a [wpide_deactivate_account] shortcode with a button members can use to close their account; they are logged out and blocked, and an administrator can reactivate them later.

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

Code

add_action( 'template_redirect', function () {
    if ( empty( $_POST['wpide_deactivate_nonce'] ) || ! is_user_logged_in() ) {
        return;
    }

    if ( ! wp_verify_nonce( sanitize_key( $_POST['wpide_deactivate_nonce'] ), 'wpide_deactivate' ) || current_user_can( 'manage_options' ) ) {
        wp_die( 'This request could not be completed.' );
    }

    $user_id = get_current_user_id();
    update_user_meta( $user_id, 'wpide_deactivated', time() );
    WP_Session_Tokens::get_instance( $user_id )->destroy_all();
    wp_clear_auth_cookie();

    wp_mail(
        get_option( 'admin_email' ),
        'A member deactivated their account',
        sprintf( "%s deactivated their account.\n%s", wp_get_current_user()->user_email, admin_url( 'user-edit.php?user_id=' . $user_id ) )
    );

    wp_safe_redirect( add_query_arg( 'account-closed', 1, home_url( '/' ) ) );
    exit;
} );

add_shortcode( 'wpide_deactivate_account', function () {
    if ( ! is_user_logged_in() || current_user_can( 'manage_options' ) ) {
        return '';
    }

    return '<form method="post" class="wpide-deactivate" onsubmit="return confirm(\'Close your account? You will be logged out.\');">'
        . wp_nonce_field( 'wpide_deactivate', 'wpide_deactivate_nonce', true, false )
        . '<p><button type="submit">Close my account</button></p></form>';
} );

add_filter( 'authenticate', function ( $user ) {
    if ( $user instanceof WP_User && get_user_meta( $user->ID, 'wpide_deactivated', true ) ) {
        return new WP_Error( 'wpide_deactivated', '<strong>This account has been closed.</strong> Contact us if you would like to reopen it.' );
    }

    return $user;
}, 60 );

add_filter( 'user_row_actions', function ( $actions, $user ) {
    if ( get_user_meta( $user->ID, 'wpide_deactivated', true ) && current_user_can( 'edit_user', $user->ID ) ) {
        $url = wp_nonce_url( admin_url( 'admin-post.php?action=wpide_reactivate&user_id=' . $user->ID ), 'wpide_reactivate_' . $user->ID );
        $actions['wpide_reactivate'] = '<strong>Deactivated</strong> · <a href="' . esc_url( $url ) . '">Reactivate</a>';
    }

    return $actions;
}, 10, 2 );

add_action( 'admin_post_wpide_reactivate', 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 do this.' );
    }

    check_admin_referer( 'wpide_reactivate_' . $user_id );
    delete_user_meta( $user_id, 'wpide_deactivated' );

    wp_safe_redirect( admin_url( 'users.php' ) );
    exit;
} );

Notes

Nothing is deleted. Deactivated users show as "Deactivated" on the Users screen; delete the wpide_deactivated user meta (or use the row link) to reactivate. Administrators cannot deactivate themselves this way.

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.