How to Trigger a Notification from an ACF Front-end Form

Updated

If you use Advanced Custom Fields (ACF) and want to trigger a notification off of a front-end form, or edit a form after it’s been submitted but before the notification is triggered, you can do the following:

Create a custom page template that contains this code:

acf_form(array(
'post_title' => true,
'post_content' => true,
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post',
'post_status' => 'draft'
),
'field_groups' => array(24), // Change this to the field group that you want to show
'submit_value' => 'Create a new post',
'updated_message' => 'Post updated'
));

Then add this to your functions.php file:

// Trigger BNFW 'Post Pending Review' notification on ACF front-end form save
function bnfw_after_save_post_pending($post_id) {
// Get the post object, post type, post status
$post = get_post($post_id);
$post_type = get_post_type($post_id);
$old_status = $post->post_status;
// Check if the post is the post type and status that we want
if ($post_type === 'post' && $old_status === 'draft') {
global $wpdb;
// Remove the action that triggers the notification in BNFW
$bnfw = BNFW::factory();
remove_action( 'pending_submissions', array( $bnfw, 'on_post_pending' ), 10, 2);
// Update the post title with the 'text' custom field from ACF and trigger the notification manually
wp_update_post(array(
'ID' => $post_id,
'post_title' => get_field('text', $post_id),
'post_status' => 'pending'
));
}
}
add_action('acf/save_post' , 'bnfw_after_save_post_pending', 20 );