1. Home
  2. /
  3. Web Design & Development
  4. /
  5. WordPress
  6. /
  7. How to Use Contact Form 7 to Save Form Data in a Custom Post With ACF Fields?

How to Use Contact Form 7 to Save Form Data in a Custom Post With ACF Fields?

How to Use Contact Form 7

To create a public feedback form on a WordPress website, you can use Contact Form 7 and Advanced Custom Fields (ACF) plugins. Configure these plugins and ensure you’re using updated versions to avoid data-saving issues. As an example, you can create a feedback form where users share their feedback, which will be listed on the front end after admin approval. Submissions will also be sent to the admin via email by default.

What is a Contact Form 7?

Contact Form 7 is a versatile and easy-to-use plugin for creating and managing contact forms on a WordPress website. With Contact Form 7, you can create custom forms with a variety of fields such as text fields, checkboxes, radio buttons, and dropdown menus. This allows you to collect the information you need from your website visitors in an organized and user-friendly way.

The plugin also provides various features to enhance the functionality of your contact forms, such as:

1. Spam filtering: Contact Form 7 uses various techniques to prevent spam submissions, including CAPTCHA and Akismet integration.

2. Form customization: You can customize the look and feel of your forms by adding custom styles and HTML, and you can also customize the email notifications sent to you and your visitors.

3. Email notifications: Contact Form 7 sends email notifications to you when a visitor submits a form, and you can customize the content and recipient of these emails.

Hence, Contact Form 7 is a powerful and flexible plugin that makes it easy to create and manage contact forms on a WordPress website.

Related: Want to implement a contact us form in shopify?

What is Custom Post With ACF Fields?

Custom Post with ACF Fields refers to a custom post type in WordPress that has additional fields added to it using the Advanced Custom Fields (ACF) plugin. Custom post types are a way to create new post types with their own custom fields, taxonomies, and capabilities, beyond the default post types like pages and posts.

With ACF, you can easily create custom fields for a custom post type, such as text fields, checkboxes, radio buttons, and more. This allows you to store additional information related to the custom post type, and display it in your theme using templates and functions provided by ACF.

In summary, Custom Post with ACF Fields is a custom post type in WordPress that has additional fields created and managed with the Advanced Custom Fields plugin, allowing you to store and display custom information related to the custom post type.

Also read: Set up a landing page or an opt-in form with Convertkit to capture emails or leads

Steps to Create a Contact Form

Using Contact Form 7 plugin, create a form with basic fields such as Name, Email, Your Story, Image, etc.

You can create the form as per your requirements.

1. Create a Page and Add The Contact Form 7 Shortcode.

Use the form shortcode which you have created in the step 1.

2. Create a Custom Post Type

You can use any plugin or do custom code in your themes functions.php file to create a custom post type. Sharing an example of how you can do so using themes functions.php file.

function create_custom_posttype(){
	
	register_post_type( 'patientstories',
        array(
            'labels' => array(
                'name' => __( 'Patient Stories' ),
                'singular_name' => __( 'Patient Story' )
            ),
			'menu_icon' => 'dashicons-welcome-write-blog',
            'public' => true,
            'has_archive' => false,
            'show_in_rest' => false,
			'supports' => array( 'title', 'editor', 'thumbnail' ),
			'publicly_queryable' => false
        )
    );
	
	
}
add_action( 'init', 'create_custom_posttype' );

3. Now, using ACF add as many fields as you have created in the contact form.

Use ACF to create a field group and attach it with the custom post type you have created in the above step.

ACF fields will carry as many fields as you want and will have all the fields for which you are collecting the data from the front-end users.

4. Handle contact form submissions on the frontend

Now, when contact form 7 is submitted .. it makes an ajax request and posts all the data to the server resulting in an email notification sent to the configured recipients.

And, since we want to capture the submitted form data and list it under custom post type. We have got a pretty easy way… ACF provides a way to update its custom field values on sharing the post id.

So, here is an example of how we can handle form submissions and store the submitted data (with status : Draft, to handle admin approval concerns).

add_action('wpcf7_mail_sent','save_stories_to_patientstories_cpt');
function save_stories_to_patientstories_cpt($contact_form){
	$submission = WPCF7_Submission::get_instance();
    if (!$submission){
        return;
    }
	$posted_data = $submission->get_posted_data();
	
	$new_post = array();
	
	if( isset($posted_data['your-name']) && !empty($posted_data['your-name']) ){
       $new_post['post_title'] = $posted_data['your-name'];
    } else {
       return;
    }
	
	$new_post['post_type'] = 'patientstories'; 
	
	if( isset($posted_data['your-story']) && !empty($posted_data['your-story']) ){
        $new_post['post_content'] = $posted_data['your-story'];
    } else {
        return;
    }
	
	$new_post['post_status'] = 'draft';
	
    if($post_id = wp_insert_post($new_post)){
		
		update_field('field_5eeaf3b4c87b1', $posted_data['email'], $post_id);
		
		$uploaded_files = $submission->uploaded_files();
	    
	    $upload_dir = wp_upload_dir(); // Set upload folder
	   	   
	    $unique_file_name = wp_unique_filename( $upload_dir['path'], $_FILES['patient_image']['name'] ); // Generate unique name
		$filename         = basename( $unique_file_name ); // Create image file name
		
		// Check folder permission and define file location
		if( wp_mkdir_p( $upload_dir['path'] ) ) {
			$file = $upload_dir['path'] . '/' . $filename;
		} else {
			$file = $upload_dir['basedir'] . '/' . $filename;
		}
	   
	   
		copy($uploaded_files['patient_image'], $file);
	   
			
	   // Check image file type
		$wp_filetype = wp_check_filetype( $filename, null );

		// Set attachment data
		$attachment = array(
			'post_mime_type' => $wp_filetype['type'],
			'post_title'     => sanitize_file_name( $filename ),
			'post_content'   => '',
			'post_status'    => 'inherit'
		);

		// Create the attachment
		$attach_id = wp_insert_attachment( $attachment, $file, $post_id );

		// Include image.php
		require_once(ABSPATH . 'wp-admin/includes/image.php');

		// Define attachment metadata
		$attach_data = wp_generate_attachment_metadata( $attach_id, $file );

		// Assign metadata to attachment
		wp_update_attachment_metadata( $attach_id, $attach_data );

		// And finally assign featured image to post
		set_post_thumbnail( $post_id, $attach_id );

    } else {
       @mail('example@example.com', 'Urgent attention needed on exampel.com share story', 'There is error in uploading share story patient images or other data. Please check.');
    }
    return;
}

With this, you will be able to handle the form submissions from the front end, store in DB, and list out under custom posts.

Also read: How to Enable cc Field in Gravity Form Email Notifications?

Benefits of Contact Form 7

Contact Form 7 is a popular plugin for WordPress, offering a range of benefits for website owners:

1. Easy to Use

It is user-friendly and easy to set up, even for those with limited coding knowledge.

2. Customizable Forms

It offers a range of customization options, allowing users to create forms that match their website design and meet their specific requirements.

3. Spam Protection

Contact Form 7 comes with built-in spam protection, helping to keep unwanted messages from reaching your inbox.

4. Widely Supported

It is a widely used plugin, with a large community of users and developers. This means that help and support are readily available.

5. Integration with Other Plugins

Contact Form 7 integrates well with other plugins, such as Advanced Custom Fields (ACF), to offer even more functionality.

6. Regularly Updated

It is regularly updated to ensure that it remains secure and effective. This means that users can be confident that the plugin will continue to meet their needs.

Related: Gravity Forms – Prevent Spam Form Submissions Using Stop Words or Disallowed Keywords

Conclusion

Using Contact Form 7 and Advanced Custom Fields (ACF) together provides a powerful solution for capturing and storing form data on a WordPress website. With its ease of use, customization options, and ability to integrate with other plugins, Contact Form 7 is a popular choice for website owners looking to capture form submissions and store them in custom posts. By combining it with ACF, users can take advantage of even more functionality and control over their form data.

By following the steps outlined in this article, users can quickly and easily set up a form that captures and stores data in a custom post with ACF fields, providing a flexible and effective solution for managing form submissions.

Rate this post
Reviews & Ratings Get your stoe online with Shopify in 60 minutes Shop Now