Support

Account

Home Forums General Issues Email User within a user field

Solved

Email User within a user field

  • I am hoping someone could point me the right direction on how I can accomplish the following:
    I have a user field on all of my posts to assign users to the posts and I would like to automatically send an email to someone when they are added in the user field. I only want the email to be sent to the new user added in the user field. Any help with this would be greatly appreciated.

  • General outline
    1) Create an acf/save_post action that fires before ACF has saved values (priority < 10)

    2) New field value will be in $_POST['acf'][$field_key]. Get old value of field using get_field(). The new value for a user field will be an array of user IDs.

    3) Compare the old value with the new value to see what users have been added to the array.

    4) Use wp_mail() to send an email to the added users.

  • I had gone in a different way but I am back to this, I know it’s been awhile. Here is what I have come up with, but it isn’t working, any ideas?

    add_action( 'acf/save_post', 'my_acf_save_hook_email', 5); function my_acf_save_hook_email( $post_id ) { 
    
    // bail early if no pds_project_manager if( empty($_POST['pds_project_manager']) ) { 
    return; }
    
    // Get the value of the ACF field $project_managers = get_field('pds_project_manager', ''); 
    // Create an empty array to store the emails $emails_to_send = array(); 
    
    // Loop through the project managers 
    foreach ($project_managers as $project_manager) { 
    // Get the user data 
    $user_data = get_userdata($project_manager); 
    
    // Check if the user already exists in the array 
    if (!in_array($user_data->ID, $emails_to_send)) { 
    // Add the user to the emails to send 
    $emails_to_send[] = $user_data->user_email; } } 
    // Send emails to the users 
    foreach ($emails_to_send as $email_to_send) { 
    // Send the email 
    wp_mail($email_to_send, 'You Have Been Added To A Project', 'You have been added to a project. Please check the project page for more information.'); } }
  • This tries to send the email but to no recipient

    add_action( 'acf/save_post', 'my_acf_save_hook_email', 5); function my_acf_save_hook_email( $post_id ) { 
    
    // bail early if no pds_project_manager 
    if( empty('pds_project_manager') ) { 
    return; }
    
    // Get the value of the ACF field 
    $project_managers = get_field('pds_project_manager', ''); 
    // Create an empty array to store the emails 
    $emails_to_send = array(); 
    
    // Loop through the project managers 
    foreach ($project_managers as $project_manager) { 
    // Get the user data 
    $user_data = get_userdata($project_manager); 
    
    // Check if the user already exists in the array 
    if (!in_array($user_data-> user_email, $emails_to_send)) { 
    // Add the user to the emails to send 
    $emails_to_send[] = $user_data->user_email; } } 
    // Send emails to the users 
    foreach ($emails_to_send as $email_to_send) { 
    // Send the email 
    wp_mail($email_to_send, 'You Have Been Added To A Project', 'You have been added to a project. Please check the project page for more information.'); } }
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.