Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Create acf_form save data and Send mail from Frontend

Create acf_form save data and Send mail from Frontend

This post will cover how to create a form on the front end of your website using Advanced Custom Field acf_form() function. We can store data as well as send email also.

To load necessary CSS, JavaScript and other stuff we need to call acf_form_head() first then using acf_form() we can display and save data and using action add_action(‘acf/save_post’, ‘my_save_post’); can send email also. code is given below and added comment to so you learn easily.

Paste below this code in your template .

<?php acf_form_head(); ?>
<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
   <?php while ( have_posts() ) : the_post(); ?>
    <?php
    $today=date("Y-m-d");
    $title="Order ".$today;
    $fields=array(
       'field_6370913bdfd02',//customer_name
       'field_636b89244cdbd',//mobile_no
       'field_636b89244cdb',//email
       'field_636b89b1f7b1e',//address
       'field_636b89bef7b1f',//pickup_date
       'field_636b8b717c297',//pick_up_time_slot
       'field_637089c9fc825'//delivery_type
    );

    acf_form(array(
        'post_id'       => 'new_post',
        //'post_title' => $title,
        'field_groups' => false,
        'fields'      => $fields,
        'new_post'      => array(
            'post_type'     => 'quick_order',
            'post_status'   => 'publish'
        ),
        'submit_value'  => 'Submit',
        'updated_message' => 'Thanks! We have received your request.',
        'return' => 'order-success',
    )); ?>
    <?php endwhile; ?>
    </div>
</div>

Now send email using code below (paste below code in functions.php.

<?php
add_action('acf/save_post', 'my_save_post');
function my_save_post( $post_id ) {
    $post = get_post( $post_id );
    
    $name = get_field('customer_name', $post_id);
    $mobile = get_field('mobile_no', $post_id);
    $emailf = get_field('email', $post_id);
    $address = get_field('address', $post_id);
    $pickD = get_field('pickup_date', $post_id);
    $pickT = get_field('pick_up_time_slot', $post_id);
    $delivT = get_field('delivery_type', $post_id);
    
    $to = '[email protected]';
    $email='[email protected]';
    $headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
    $subject = "Quick Order";
    $body = '';
    $body .="Customer Name : ".$name."<br/>";
    $body .="Mobile No. : ".$mobile."<br/>";
    $body .="Email ID : ".$emailf."<br/>";
    $body .="Address : ".$address."<br/>";
    $body .="Pickup Date : ".$pickD."<br/>";
    $body .="Pickup Time : ".$pickT."<br/">;
    $body .="Delivery Type : ".$delivT."<br/";
    
    wp_mail($to, $subject, $body, $headers );
}
?>
4 2 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments