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() ) {
	// 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 = '';
	$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>
            " ;
	// send email
	wp_mail($to, $subject, $body, $headers );

Comments on this post

No comments.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL