Halloween Sale 15% off Ends in See the plans
PHP

"Delete My Account" Request Button

Adds a [wpide_delete_account] shortcode with a button that files an erasure request in WordPress's privacy tools; the member confirms by email and you process it under Tools > Erase Personal Data.

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

Code

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

    if ( ! wp_verify_nonce( sanitize_key( $_POST['wpide_erase_nonce'] ), 'wpide_erase_request' ) ) {
        wp_die( 'The form expired. Please go back and try again.' );
    }

    $user       = wp_get_current_user();
    $request_id = wp_create_user_request( $user->user_email, 'remove_personal_data' );

    if ( is_wp_error( $request_id ) ) {
        $state = 'duplicate_request' === $request_id->get_error_code() ? 'pending' : 'error';
    } else {
        wp_send_user_request( $request_id );
        $state = 'sent';
    }

    wp_safe_redirect( add_query_arg( 'erase-request', $state ) );
    exit;
} );

add_shortcode( 'wpide_delete_account', function () {
    if ( ! is_user_logged_in() ) {
        return '';
    }

    $messages = array(
        'sent'    => 'Check your email: we sent a link to confirm the deletion request.',
        'pending' => 'You already have a deletion request in progress. Check your email for the confirmation link.',
        'error'   => 'Something went wrong. Please contact us to delete your account.',
    );
    $state = isset( $_GET['erase-request'] ) ? sanitize_key( $_GET['erase-request'] ) : '';

    if ( isset( $messages[ $state ] ) ) {
        return '<p class="wpide-erase-notice" role="status">' . esc_html( $messages[ $state ] ) . '</p>';
    }

    return '<form method="post" class="wpide-erase-form" onsubmit="return confirm(\'Request deletion of your account and personal data?\');">'
        . wp_nonce_field( 'wpide_erase_request', 'wpide_erase_nonce', true, false )
        . '<p>Want us to delete your account and personal data? We will email you to confirm.</p>'
        . '<p><button type="submit">Request account deletion</button></p></form>';
} );

Notes

Nothing is deleted automatically; it uses the core personal-data erasure workflow so you stay in control.

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.