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

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:

1
2
3
4
5
6
7
8
9
10
11
12
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 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 );

Subscribe for BNFW News

Want to know when new add-ons or features are released? Sign up to the newsletter. No spam, ever. Unsubscribe at any time.

Please enter your name.
Please enter a valid email address.
Something went wrong. Please check your entries and try again.