• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Blog
    • Basics
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Store
    • Cart
    • Checkout
  • Contact
  • Log In
  • Show Search
Hide Search
Home » Code Snippets » Allow Users to Delete Account (with confirmation message)

Allow Users to Delete Account (with confirmation message)

Chad Butler · Nov 21, 2017 ·

<?php

/**
 * User self delete script.
 * 
 * This adds a "delete my account" link to the user profile links
 * created with the [wpmem_profile] shortcode. The user selects and
 * confirms to delete their account and is presented with a success
 * message when the account is deleted.
 */


// Add below this line to functions.php


/**
 * Checks to see if a user is self deleting and if so, after
 * verifying the nonce and form post fields for validity, 
 * the user is deleted.
 */
add_action( 'wpmem_after_init', 'my_user_self_delete' );
function my_user_self_delete () {
    // Verify that the user intended to take this action.
    if ( isset( $_POST['self_delete'] ) && 'delete' == $_POST['self_delete'] ) {
        $nonce = ( isset( $_REQUEST['_wpnonce'] ) ) ? $_REQUEST['_wpnonce'] : false;
        if ( ! wp_verify_nonce( $nonce, 'wpmem_self_delete' ) ) {
            return;
        }
        require_once( ABSPATH . 'wp-admin/includes/user.php' );
        $current_user = wp_get_current_user();
        $wpmem_self_delete = array(
            'self_delete' => 'complete',
            'username' => $current_user->user_login,
        );
        wp_delete_user( $current_user->ID );
        wp_redirect( add_query_arg( $wpmem_self_delete, wpmem_profile_url() ) );
        exit();
    }
}

/**
 * If a user deleted their account, display a success message.
 */
add_filter( 'the_content', 'my_user_self_delete_msg', 999 );
function my_user_self_delete_msg($content){

    if ( isset( $_GET['self_delete'] ) && 'complete' == $_GET['self_delete'] ) {
        $content = wpmem_inc_regmessage( 'self_delete', 'Account deleted for: ' . sanitize_user( $_GET['username'] ) ) . $content;
    }
    return $content;
}


/**
 * This is the member links framework. This adds the user delete
 * link to the member links.  If that action is selected, this
 * displays the form button to validate user deletion.
 */
add_filter( 'wpmem_member_links', 'my_custom_member_links' );
function my_custom_member_links( $links ) {
    
    // Settings.
    $link_text   = 'Delete My Account';
    $return_text = 'Return to User Settings';
    
    /*
     * You probably don't need to change anything below here.
     */
    $process_name = 'delete_my_account'; 
    $return_link  = remove_query_arg( 'a' );
    $return_link  = sprintf( '<p><a href="%s">%s</a></p>', esc_url( $return_link ), $return_text );
    
    // If the page is the custom process page, build and display the form.
    if ( ( isset( $_REQUEST['a'] ) && $process_name == $_REQUEST['a'] ) ) {

        $content = '<p>Do you really want to delete your account?<br />This action is permanent and cannot be undone!</p>
            <p><form name="form" method="post" action="' . esc_url( wpmem_profile_url() ) . '">'
                . wp_nonce_field( 'wpmem_self_delete' )
                . '<input type="hidden" name="self_delete" value="delete" />'
                . '<input type="submit" name="submit" id="submit" class="button button-primary" value="' . $link_text . '"  />'
                . '</form></p>';
        
        // Add a return button.
        $links = $content . $return_link;

    } else {

        $link = add_query_arg( 'a', $process_name, wpmem_profile_url() );
        $new_link = '<li><a href="' . esc_url( $link ) . '">' . $link_text . '</a></li>';
        
        // Add new link at the end of the existing list.
        $links = str_replace( '</ul>', $new_link . '</ul>', $links );
        
    }
    
    return $links;
  }

Not sure what to do with this code?

You're not a "coder" and don't know what to do? Don't worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.

Here are some free articles to get you started:

  • Using Code Snippets from the Site
  • The functions.php File
  • Create a plugin file for custom functions
  • Create a child theme
  • Do not modify plugin files!

For "hands on" help, consider a plugin support subscription or the Pro Bundle.

Ready to get started?

Join Today!

© 2023 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Privacy Policy
  • Terms of Service
  • Refund Policy