acf-form-shortcode
acf-form-shortcode copied to clipboard
Feature Request: Email Support
The ability to send an email to a recipient when the frontend, acf-form is submitted. Setting the various variables (email from, subject, message and other possible headers, etc).
Many uses for acf_form() tend to be for allowing users to update certain info from the front end. It is reasonable that most site admins would like to be notified when such an action is completed.
Below is what I have now running that allows me to do this...with the added extras I needed. It is pretty simple actually and would be really easy to implement in terms of your plugin in conjunction with ACF. If a basic settings page were added for the plugin, fields therein could just be filled in and only be active with an "IF" statement is fulfilled. Alternatively, to be more flexible...you could create a custom section on each page/post edit screen that has those fields. Essentially doing what I do with ACF really...but your plugin would just take care of everything rather than requiring users to create a new field group, etc. Just some ideas.
// Updates FYI last update timestamp when custom field is updated and notify admin
function my_acf_save_post( $post_id ) {
// bail out early if we don't need to update the date
if( is_admin() || $post_id == 'new' ) {
return;
}
global $wpdb;
$datetime = date("F j, Y g:i a");
$query = "UPDATE $wpdb->posts
SET
post_modified = '$datetime'
WHERE
ID = '$post_id'";
$wpdb->query( $query );
$print_approved = get_field('approved_by');
$approved_email = get_field('approval_email');
$to = '[email protected]';
$subject = get_the_title($post_id) .' Design Approved';
$message = 'The design was approved by '. $print_approved .' ('.$approved_email.') to be sent to print.';
add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');
function new_mail_from($old) {
return '[email protected]';
}
function new_mail_from_name($old) {
return 'Spiderfly Studios';
}
if ( $approved_email && $print_approved ) {
wp_mail( $to, $subject, $message );
}
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
Hi Spiderfly.
I promise I will consider it a feature. It may be smarter to just keep it clean tho. If you install a plugin that's supposed to add shortcode functionality to acf_form
you don't really expect it to also send out emails for you. I'd consider that weird and code bloat 90% of the time. Especially when you can do it yourself with just a few lines of code (as you've proven yourself).
Here's a bit cleaner version of your functionality tho. No add_filter
or function within functions (avoid that).
/**
* Updates FYI last update timestamp when custom field is updated and notify admin
*
*/
function send_approval_email( $post_id ) {
// bail out early if we don't need to update the date
if( is_admin() || $post_id == 'new' ) {
return;
}
global $wpdb;
$datetime = date("F j, Y g:i a");
$query = "UPDATE $wpdb->posts
SET post_modified = '$datetime'
WHERE ID = '$post_id'";
$wpdb->query( $query );
$print_approved = get_field('approved_by');
$approved_email = get_field('approval_email');
//No need to keep going if these don't exist
if ( !$approved_email || !$print_approved ) {
return;
}
$to = '[email protected]';
$subject = get_the_title($post_id) .' Design Approved';
$message = '<p>' . sprintf( __( 'The design was approved by %1$s (%2$s) to be sent to print.' ), $print_approved, $approved_email ) . '</p>';
$headers = array(
'From: Spiderfly Studios <[email protected]>',
'Content-Type: text/html; charset=UTF-8' // Set html email
);
wp_mail( $to, $subject, $message, $headers );
}
add_action('acf/save_post', 'send_approval_email', 20);