Home › Forums › Front-end Issues › Trigger Email On Front End Form Update › Reply To: Trigger Email On Front End Form Update
Hi @prithsr
Ok, if I’ve understood right, here’s how I think I’d possibly approach it:
I’d use the acf/save_post function:
add_action('acf/save_post', 'my_acf_save_post');
function my_acf_save_post( $post_id ) {
// Get newly saved values.
$values = get_fields( $post_id );
// Get post author ID
$author_id = get_post_field ('post_author', $post_id);
// Add a custom field to the user
update_user_meta( $author_id, 'some_custom_field', '1' );
}
You could then query the users via a php file you run on cron:
#query our users
$args = array(
'role__in' => array('editor'),
'role__not_in' => array('administrator'),
'exclude' => array(1),
'meta_query' => array(
array(
'key' => 'some_custom_field',
'value' => 1
)
)
);
$user_query = new WP_User_Query( $args );
$user = get_userdata( $user->ID );
You could wrap the whole query in a date check to compare the current date with the date set by the user
If the date matches and the query returns a result, you can then reset the custom meta for the user/author:
// Reset custom field to the user
update_user_meta( $user->ID, 'some_custom_field', '0' );
So roughly the email function could look like:
###################################################
# Send Emails When custom_post_typePublished
###################################################
add_action( 'transition_post_status', 'send_email_notification', 10, 3 );
function send_email_notification( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status or 'publish' === $old_status || 'custom_post_type' !== get_post_type( $post ) )
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
#get the post ID, we can now use this on both publish and update functions
$post_id = $post->ID;
#add date check here
$todays_date = '';
$specific_date = '';
if ($todays_date == $specific_date ) {
#query our users
$args = array(
'role__in' => array('subscriber'),
'role__not_in' => array('administrator'),
'exclude' => array(1),
'meta_query' => array(
array(
'key' => 'some_custom_field', //only want to send to specific users
'value' => '1'
)
)
);
$user_query = new WP_User_Query( $args );
#get our email data if we have any results
if ( ! empty( $user_query->get_results() ) ) {
#create an array
$email_recipients = array();
foreach ( $user_query->get_results() as $user ) {
$user = get_userdata( $user->ID );
$email_recipients[] = array(
'forename' => $user->first_name,
'email' => $user->user_email
);
update_user_meta( $user->ID, 'some_custom_field', '0' ); //reset the custom meta until front end form retriggers
}
}
#filter duplicates (may not need this)
#$email_recipients = array_unique($email_recipients);
#proceed with the email if we have a result
if ($email_recipients) {
foreach ($email_recipients as $recipients) {
$to = $recipients['email'];
$subject = "Email Subject Goes Here";
$message = "<p>Dear {$recipients['forename']}</p>";
#create the headers
$headers = array(
'Content-Type: text/html; charset=UTF-8',
#'Cc:' . '[email protected]',
#'Bcc:' . '[email protected]',
'From: '.get_option( 'blogname' ).' <[email protected]>'
);
#send the email
wp_mail ($to, $subject, $message, $headers);
}
}
}
}
It may require some spit and polish but should be a rough starting point I hope!?
Cheers
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.