Support

Account

Home Forums Front-end Issues Trigger Email On Front End Form Update

Solving

Trigger Email On Front End Form Update

    • jarvis

    • December 19, 2016 at 8:26 pm

    Hi All,
    I’m really hoping someone can help as the below is driving me mad!!!

    I have a page that has 2 forms, depending on a condition, depends which form is displayed (works fine).

    These are the 2 forms:

    					acf_form(array(
    						'field_groups' => array(63),
    						'post_id' => 'user_'.get_current_user_id(),
    						'submit_value'	=> 'Update Premium Details',
    						'updated_message' => __("Premium Details Updated", 'acf'),
    						'html_after_fields' => '<input type="hidden" name="acf[update]" value="true"/>',
    					)); 

    AND

    					acf_form(array(
    						'field_groups' => array(183),
    						'post_id' => 'user_'.get_current_user_id(),
    						'submit_value'	=> 'Update Standard Details',
    						'updated_message' => __("Standard Details Updated", 'acf'),
    						'html_after_fields' => '<input type="hidden" name="acf[update]" value="true"/>',
    					)); 

    I then have the following in my functions file:

    add_action('acf/save_post', 'my_save_post');
    
    function my_save_post( $post_id ) {
    		
    	# add conditional to check form was triggered
    	if(($_POST['acf']['update'] == 'true')) {
    	
    	# bail early if editing in admin
    	if( is_admin() ) {		
    		return;		
    	}
    		
    	# vars
    	$post = get_post( $post_id );	
    	
    	# get custom fields (field group exists for content_form)
    	# need to extract user ID
    	$user_id = str_replace("user_", "", $post_id);	
    	
    	# get user registration details
    	$user_info = get_userdata($user_id);
    	$first_name = $user_info->first_name;
    	$last_name = $user_info->last_name;
    	$user_email = $user_info->user_email;
    	$user_login = $user_info->user_login;
    	
    	$active = get_field('display_in_directory', $post_id);
    	if ($active == true ) {
    		$status = 'Yes';
    	} else {
    		$status = 'No';	
    	}	
    	
    	# email data
    	$to = get_bloginfo('admin_email');
    	$headers = 'From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <' . $user_email .'>' . "\r\n";
    	$subject = 'Profile Updated';
    	$body = $user_info->first_name. ' '.$user_info->last_name. ' has updated their profile' . "\r\n";
    	$body .= 'They are registered with a username name of ' . $user_login . "\r\n";
    	$body .= 'Active in directory? ' . $status . "\r\n";
    		
    	# send email
    	wp_mail($to, $subject, $body, $headers );
    	
    	}
    	
    }

    Now, when I update the page with the 2nd form (‘field_groups’ => array(183),) the function triggers and I get the email

    Yet when I update the page with the 1st form (‘field_groups’ => array(63),)
    the email never gets triggered

    For the life of me, I can’t see why?

    Any pointers would be very much appreciated!!!

    Thanks in advanced

    • James

    • December 21, 2016 at 4:47 pm

    Hi @jarvis

    Because this is related to the wp_mail() function, I think the easiest way to find the issue is to check your PHP error log. Could you please check it and see if there are any related error messages?

    Also, could you please share the JSON or XML export file of your field group so I can check your setup?

    Thanks 🙂

    • jarvis

    • December 21, 2016 at 11:55 pm

    Thanks @acf-support

    I managed to work it out!

    I added:

    
    	$mailResult = false;
    	#$mailResult = wp_mail( 'my@email.com', 'test if mail works', 'hurray' );
    	echo $mailResult;	
    

    I then worked out I had a typo!
    This line:
    $headers = 'From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <' . $user_email .'>' . "\r\n";

    Should be:
    $headers = 'From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <' . $user_info->user_email .'>' . "\r\n";

    Basically, the email didn’t have $user_info-> in front of it!

    Now working

    Cheers

    • jarvis

    • December 22, 2016 at 12:31 am

    Apologies, I missed this part:
    $headers = array('From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <'.$user_info->user_email.'>');
    Notice the header is an array!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.