Hello,
I am working with WordPress and ACF Pro Fields.
I want to let users fill out some form, after submit, I want to receive an E-Mail with all the information the user put in AND a specific posttype should be created in the WordPress backend(draft) so I can review it and then publish ist.
All this works perfectly fine!
BUT when I updated the post, I receive a NEW E-Mail with the same information and the updated content, I don’t want that, how can I stop this?
Here is my Code
Form:
<?php acf_form(array(
'field_groups' => array('group_5fc8d223090ce'),
'form' => false,
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'my_posttype',
'post_status' => 'draft',
'post_content' => true,
),
)); ?>
<input type="submit" id="issubmitform" name="issubmitform"
class="acf-button button button-primary button-large" value="Submit">
My function:
add_action('acf/save_post' , 'my_save_post');
function my_save_post($post_id) {
if( get_post_type($post_id) !== 'my_posttype' ) {
return;
}
add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
$name = get_field('my_acf_field', $post_id);
$to = '[email protected]';
$headers = "Content-Type: text/html; charset=UTF-8";
$subject = 'My Subject' . $name;
$message = "This is the message"
wp_mail($to, $subject, $message, $headers );
remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
};
function my_acf_save_post3($post_id) {
if ($_POST['issubmitform'] === "Submit"){
wp_redirect( 'https://mywebsite.com/thankyou/' ); exit;
}
}
add_action('acf/save_post', 'my_acf_save_post3', 20);
I am sure I am just missing something, any help would be appreciated
I had the same “issue”.
I created a work around with an extra ACF field which will be filled on the first time after saving. For sending the email i check if that field is filled. If its empty, its a new post otherwise its an updated post
if(empty(field)) { send email + fill field }
check the post status and only send the email if it is draft.
Hi John
Would you please explain how we can check the post status?
I have exactly the same issue.
After a form is submitted a new CPT-entry is made as draft.
The email goes out to the client who has filled that form.
After reviewing and publishing the post, the mail goes out for a second time.
On submit I have a mail like “thank you, we will check and publish”.
After publishing it would be great to have another mail like “your post is published now” … or at least not the same mail again.
Thank you
For publishing the post you would not use an ACF hook, you would use the WP transition_post_status hook.
Hi John
Thank you for helping me out.
To make it more precise:
I have a frontend form where anyone can add a post to a cpt.
This post is saved as draft until the site-owner is publishing the post.
After filling the form on frontend, a mail should go to the client with a thank you message that his post will be published if everyting is ok.
Thats where I have used acf/save_post.
This is my Code
add_action('acf/save_post' , 'my_save_post', 20);
function my_save_post($post_id) {
// If not CPT, exit
if( get_post_type($post_id) !== 'inserate' ) {
return;
}
add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
// get custom fields (field group exists for content_form)
$name = get_field('name', $post_id);
$email = get_field('email', $post_id);
$to = $email;
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$subject = 'Thank you';
$message = 'Thank you for submitting a post';
wp_mail($to, $subject, $message, $headers );
remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
}
Now my question is, to just send this mail after submitting the form, but not after publishing. At the moment the action is fired multiple times and I need a check for the post status or something else.
Roman
You check the post status using get_post_status(), which will == ‘draft’ for a draft post.
Hoi John
Thank you, that helped me alot and I have now managed it.
My biggest mistake was, that I was not saving as draft but as pending.
Thank you very much
Roman
You must be logged in to reply to this topic.
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.