Support

Account

Home Forums ACF PRO acf/save_post – send email only once the posttype is created

Unread

acf/save_post – send email only once the posttype is created

  • 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 = 'mymail@gmail.com';
        $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

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.