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', 'ab_notify_acf_submit');
function ab_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 ACF form)
$fields = '';
if(get_field('name', $post_id)){
$name = get_field('name', $post_id);
$fields .= $name . '<br>';
}
if(get_field('email', $post_id)){
$email = get_field('email', $post_id);
$fields .= $email . '<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 = get_option('admin_email');
$to = 'your@email.com';
$headers = array('Content-Type: text/html; charset=UTF-8');//make it HTML
$subject = 'A new submission from the'. bloginfo('name');
$link = get_edit_post_link( $post_id);
$body = "<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<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}
</body>
</html>";
// send email
wp_mail($to, $subject, $body, $headers );
}
Last Update on:March 5th, 2023 at 5:19 pm