0

I have a custom function in my plugin, I want to run it when any of the wordpress user is deleted so i can know that a user and which user was deleted. I implemented this but it is'nt working.

 function my_delete_user( $user_id ) {
    global $wpdb;
    require_once(ABSPATH.'wp-admin/includes/user.php' );
        $user_obj = get_userdata( $user_id );
        $email = $user_obj->user_email;

    $headers = 'From: ' . get_bloginfo( "name" ) . ' <' . get_bloginfo( "admin_email" ) . '>' . "\r\n";
      wp_mail( $email, 'You are being deleted, brah', 'Your account at ' . get_bloginfo("name") . ' is being deleted right now.', $headers );
}
add_action( 'delete_user', 'my_delete_user' );

but this is not working, as it should send a mail. I want to know which user was deleted from their email.

  • You're setting $email to the user's email. So you're emailing them when their account is deleted. wp_mail() should us your email address as the first argument if you want to know when a user is deleted. – Jacob Peattie Jan 11 at 4:53
  • @JacobPeattie I created a user with the email i use but the wp_mail() is not sending any mail to my email, so should I believe that the function is not working? – Biraj Gautam Jan 11 at 4:57
  • Is that the account that you deleted? – Jacob Peattie Jan 11 at 5:04
  • a WP user account (subscriber), yes. – Biraj Gautam Jan 11 at 5:09
  • 1
    The first step is to prove whether or not your hook is working. Make this the first line of your function echo 'success!'; die(); - if your function is hooked in properly, when you delete a user you should get a blank screen with just 'success' at the top. – Peter HvD Jan 11 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.