<?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 ( 'delete' == wpmem_get( 'self_delete', false ) ) { if ( ! wp_verify_nonce( wpmem_get( '_wpnonce', false, 'request' ), '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 ( 'complete' == wpmem_get( 'self_delete', false, 'get' ) ) { $content = wpmem_get_display_message( '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 ( $process_name == wpmem_get( 'a', false, 'request' ) ) { $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
- Using a code snippets plugin
- 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.