Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //delete user
- function delete_my_user_account($request) {
- if ( is_user_logged_in() ) {
- // Can't delete admin accounts
- if ( current_user_can( 'manage_options' ) ) {
- wp_send_json( array(
- 'status' => 'fail',
- 'title' => __( 'Error!', 'wp-delete-user-accounts' ),
- 'message' => __( 'Administrators cannot delete their own accounts.', 'wp-delete-user-accounts' )
- ) );
- }
- // Get the current user data
- $user_id = get_current_user_id();
- // Get user meta data
- $meta = get_user_meta( $user_id );
- // Delete user's meta data
- foreach ( $meta as $key => $val ) {
- delete_user_meta( $user_id, $key );
- }
- // User Logout
- wp_logout();
- if ( ! function_exists( 'wp_delete_user' ) ) {
- require_once(ABSPATH.'wp-admin/includes/user.php' );
- }
- // Delete the user's account
- $deleted = wp_delete_user( $user_id );
- if ( $deleted ) {
- // Success
- return array(
- 'status' => 'success',
- 'title' => __( 'Success!', 'wp-delete-user-accounts' ),
- 'message' => __( 'Your account was successfully deleted. Fair well.', 'wp-delete-user-accounts' )
- ) ;
- } else {
- return array(
- 'status' => 'fail',
- 'title' => __( 'Error!', 'wp-delete-user-accounts' ),
- 'message' => __( 'Request failed.', 'wp-delete-user-accounts' )
- ) ;
- }
- }
- }
- add_action('rest_api_init', function () {
- register_rest_route( 'remove_user/v1', 'user/me',array(
- 'methods' => 'DELETE',
- 'callback' => 'delete_my_user_account'
- ));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement