How to Allow WordPress Users to Delete Accounts




Geniuswp show

Summary: Unless otherwise noted, any code snippet in this post goes in your theme's functions.php file or a plugin for your custom functions.UPDATE: The plugin from the tutorial is now available on the WordPress plugin repository – WP Delete User AccountsSome websites allow a user to delete his or her account, and many don’t. Personally, I like having the option for deleting my accounts (i.e. social media), and I’m sure many other people do, because it gives us a sense of “control.”If your website runs on WordPress, you may know that there is no built-in method for allowing your users to delete their accounts. When an account is created, either by the admin or the user, the only way for that account to be deleted is for an administrator to manually delete the account via the WordPress admin, or by directly removing it from the database (not recommended, unless you know what you’re doing).In this tutorial, we’ll set up a method for allowing our users to delete their accounts on our WordPress websites. Since deleting a user’s account is serious business, for which there is no return, unless you restore a backup of the database, we’ll want to implement some extra security features. Here’s what we’ll cover:Create a shortcode that will output the delete-account button. This will be for allowing users to delete their accounts on the frontend, such as through an account page you have set up.The delete-account button will prompt the user to confirm his or her action through a modal window that requires user input.After the prompt is submitted successfully, we’ll send the request to delete the account via AJAX. When the deletion is processed, we’ll send a response, display it to the user, then redirect the user to our home page.Add the delete button on the admin profile page. This will be for deleting an account while in the admin.If you want to download or look at the code before getting started (or you just want the code), you can grab it from GitHub.Create the ButtonFirst, let’s start out easy by covering the button we’ll be using to submit the request. Under the “includes” directory, there’s a file called functions.php, which is not to be mistaken for your theme’s functions.php. This file will hold the plugin’s helper functions, which, as of now, is only one function. Here’s that function:function wp_delete_user_account_delete_button( $button_text = '' ) {// Bail if user is logged outif ( ! is_user_logged_in() ) {return;}// Bail to prevent administrators from deleting their own accountsif ( current_user_can( 'manage_options' ) ) {return;}// Defauly button textif ( $button_text == '' ) {$button_text = __( 'Delete My Account', 'wp-delete-user-accounts' );... You are listening to the topic about "How to Allow WordPress Users to Delete Accounts", if you want to read the full article, please visit https://geniuswp.com or the link in the description.