Send an Email on ACF Form Submission

The following code emails a particular person whenever an ACF Form is submitted. It’s a slight modification of the example at the bottom of this page.

Since I’m dealing with three forms with different fields, I do some if statements to gather and append various fields if they exist.

In this particular workflow, all these post types end up in draft mode so I use get_edit_post_link to put the approver (the person getting this email) into the editor view rather than using get_the_permalink (which would take them to the preview).

Since I wanted a bit more control, the email is also set to be HTML via the headers – $headers = array(‘Content-Type: text/html; charset=UTF-8’);

And finally, is_admin() prevents the email from firing if you’re creating content in the normal editor zone rather than via the form. I am not a fan of the name of this function as I have forgotten and used it to try to see if someone is an editor (which it doesn’t do). Feels like it should be is_in_admin or something like that.

add_action('acf/save_post', 'ar_notify_acf_submit');

function ar_notify_acf_submit( $post_id ) {	
	
	//bail early if editing in admin rather than the front end of the site via the form
	if( is_admin() ) {
		return;	
	}
		
	
	// get custom fields (field group exists for content_form)
    $fields = '';
    if(get_field('name', $post_id)){
        $name = get_field('name', $post_id);
        $fields .= $name . '<br>';
    }
    if(get_field('short_biography', $post_id)){
        $bio = get_field('short_biography', $post_id);
        $fields .= $bio . '<br>';
    }
	if(get_field('summary', $post_id)){
        $summary = get_field('summary', $post_id);
        $fields .= $summary . '<br>';
    }
    if(get_field('link', $post_id)){
        $link = get_field('link', $post_id);
        $fields .= "<a href='{$link}'>{$link}</a>";
    }
	
	
	// email data
	$to = 'someone@middlebury.edu';
	$headers = array('Content-Type: text/html; charset=UTF-8');//make it HTML
	$subject = 'A new submission from the Antiracist site!';
    $link = get_edit_post_link( $post_id);
	$body = "<p>You can edit and approve it at the following link. </p>
            <p><a href='{$link}'>{$link}</a></p>
            <h2>Partial Preview</h2>
            {$fields}
            " ;
	
	// send email
	wp_mail($to, $subject, $body, $headers );
	
}

7 thoughts on “Send an Email on ACF Form Submission

  1. Hello ?

    Thank you for this snippet. Really useful and work great! ??

    There is only one issue, it is sending two different emails: one with the link to “edit and approve” the post just submitted, but also another one to “edit and approve” the custom post type where the post is being created.

    Any chance you could help me to edit the code to avoid this double submission?

    Love! ?

      1. Thanks for your reply 🙂

        I was able to pick another piece of code from another post about the subject, and added it to your code in order to get only one email, this is the code:

        // Return if not a post
        if( get_post_type($post_id) !== ‘companies’ ) { // here ‘post’ will be your post type

        return;

        }

        For some reason, I was getting the notification for the custom post type “companies”, but also a email with the link to the page where the ACF submission form is.

Comments are closed.