Advertisement
mrblab

theme_functions.php

Apr 10th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. //delete user
  2. function delete_my_user_account($request) {
  3.     if ( is_user_logged_in() ) {
  4.         // Can't delete admin accounts
  5.         if ( current_user_can( 'manage_options' ) ) {
  6.             wp_send_json( array(
  7.                 'status' => 'fail',
  8.                 'title' => __( 'Error!', 'wp-delete-user-accounts' ),
  9.                 'message' => __( 'Administrators cannot delete their own accounts.', 'wp-delete-user-accounts' )
  10.             ) );
  11.         }
  12.  
  13.         // Get the current user data
  14.         $user_id = get_current_user_id();
  15.  
  16.         // Get user meta data
  17.         $meta = get_user_meta( $user_id );
  18.  
  19.         // Delete user's meta data
  20.         foreach ( $meta as $key => $val ) {
  21.             delete_user_meta( $user_id, $key );
  22.         }
  23.  
  24.  
  25.         // User Logout
  26.         wp_logout();
  27.  
  28.         if ( ! function_exists( 'wp_delete_user' ) ) {
  29.             require_once(ABSPATH.'wp-admin/includes/user.php' );
  30.         }
  31.  
  32.         // Delete the user's account
  33.         $deleted = wp_delete_user( $user_id );
  34.  
  35.  
  36.         if ( $deleted ) {
  37.  
  38.             // Success
  39.             return array(
  40.                 'status' => 'success',
  41.                 'title' => __( 'Success!', 'wp-delete-user-accounts' ),
  42.                 'message' => __( 'Your account was successfully deleted. Fair well.', 'wp-delete-user-accounts' )
  43.             ) ;
  44.  
  45.         } else {
  46.  
  47.             return  array(
  48.                 'status' => 'fail',
  49.                 'title' => __( 'Error!', 'wp-delete-user-accounts' ),
  50.                 'message' => __( 'Request failed.', 'wp-delete-user-accounts' )
  51.             ) ;
  52.         }
  53.     }
  54. }
  55.  
  56.  
  57. add_action('rest_api_init', function () {
  58.   register_rest_route( 'remove_user/v1', 'user/me',array(
  59.                 'methods'  => 'DELETE',
  60.                 'callback' => 'delete_my_user_account'
  61.       ));
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement