0

Is there a way or a snippet that can be created to automatically send an automatic email to users who we manually approve stating there account has been approved.

To go a little in-depth about this - User's are able to sign up to our website and the information is sent to the website, we have a custom plugin that display certain information the user entered, the information is displayed through using Advanced Custom Fields. In the customised plugin section we have "login allowed" checkbox, i want it so when i check this and click update user, the email is sent.

The email would be automatically retrieved from the wordpress's default "Basic information" section if possible. Any guidance / clarification & feedback is majorly appreciated.

EDIT:

add_action(‘acf/save_post’, ‘my_save_post’);

function my_save_post( $post_id ) {

// create some logic here to check if you are editing a user
global $pagenow;
if ($pagenow == ‘user-edit.php’) {

//get the value of the field
$value = get_field(‘login_allowed’,$post_id);

// check if the checkbox is filled
if ( ! $value ='unchecked' ) {
        return false;
    }

    $value == 'checked'{

// Company information
        $email = “removed”;
        $name = “removed”;

        //get user's email
        $user = get_user_by('email', $useremail);
        if ($user) {
        $details['email'] = $user->user_email;


        // email data
    $to = $useremail;
    $subject = 'The subject';
    $body = 'The email body content';
    $headers = ‘From:‘ . $name . ‘ <‘ . $email . ‘>’ . “\r\n”;

        // send email
        wp_mail($to, $subject, $body, $headers );

        }
      }
    }
  }

1 Answer 1

0

First, add a custom Action Hook that is triggered whenever the user is updated.

For example, when custom meta fields are updated in WordPress, some action hooks such as: updated_{$meta_type}_meta, updated_postmeta, etc.

So if you're using the ACF plugin, you can hook into the 'acf/save_post' action hook. The action hook lets you do something before or after the custom field are being updated. For more info on using the acf/save_post, click here.

Next, you can now send the email to the user using the wp_mail() function.

Based on the code you have provided, I made some refactoring:

add_action(‘acf/save_post’, ‘my_save_post’);

function my_save_post( $post_id ) {

// create some logic here to check if you are editing a user
// Keep an eye on this pagenow check to see if it's correct
// Use wp_die( var_dump( $pagenow ); to debug
global $pagenow;
if ($pagenow == ‘user-edit.php’) {

//get the value of the field
$value = get_field(‘login_allowed’,$post_id);

// Remove this, just here for debugging
// wp_die( var_dump( $value ) );

// check if the checkbox is filled
if ( $value == 'unchecked' ) {
        return false;
}

        // Company information
        $email = “removed”;
        $name = “removed”;

        // Debug: I see $useremail variable, is it set somewhere?

        //get user's email
        $user = get_user_by('email', $useremail);
        if ($user) {
        $details['email'] = sanitize_email( $user->user_email );


        // email data
    $to = $useremail;
    $subject = 'The subject';
    $body = 'The email body content';
    $headers = ‘From:‘ . $name . ‘ ’ . “\r\n”;

        // send email
        wp_mail($to, $subject, $body, $headers );

        }
      }
  }
7
  • Hi John thanks for your reply - I actually forgot to put my snippet code i shall edit the post and add it in now, any guidance is appreciated on it.
    – YxngLBZ
    Commented May 21, 2020 at 12:47
  • Hello @YxngLBZ, I have updated the my example with your code being refactored Commented May 21, 2020 at 19:43
  • As I've explained, if nothing is happening, try using wp_die( var_dump( $variable_here ) ); to inspect the code Commented May 21, 2020 at 19:44
  • Regarding your message about $usermail - I was unsure how to retrieve the email automatically form the basic information section so i just created it, i believe it may not work though.
    – YxngLBZ
    Commented May 22, 2020 at 11:17
  • If you're editing the user profile, there's always a reference to the User ID, User Email or User Login. Mind you, as stated on the ACF official doc, you cannot retrieve some editable object by hooking into the acf/save_post action hook. So now, you will have to use the native metadata update action hooks that gets fired every single time a meta field is updated. Commented May 22, 2020 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.