How to process data sent with a form Contact Form 7
Contact Form 7 is the most popular WordPress plugin for quickly creating forms that you can place on a website. It is fast, safe and fulfils its role. What’s more – thanks to the clever use of filters and actions, it allows easy modifications and integration.
What is Contact Form 7?
Contact Form 7 (CF7) is a WordPress plugin for creating contact forms and collecting data from website visitors. It’s the most popular contact form plugin, with over 5 million installations worldwide, due to its simplicity, reliability, and flexibility.
It offers an easy-to-use interface to quickly create contact forms that match your website’s look and feel. You can customise your contact form with additional features such as reCAPTCHA spam protection, multiple recipients, and automatic notification messages.
CF7 provides an easy way to collect contact information from your website visitors without manually entering it into a database. All forms created by CF7 are fully customisable and come with various settings and options; you can:
- choose the fields you want to include in the contact form,
- decide who should receive contact inquiries,
- adjust the email notifications sent out when someone submits a contact form or even
- set up conditional logic rules to trigger different actions based on user input.
In addition to the core functionality, Contact Form 7 is extensible via plugins and themes to customise your contact form further.
Many available add-ons allow you to customise the look and feel of your contact form or integrate it with popular services like MailChimp or Zapier.
You can connect CF7 with external services like Google ReCaptcha for extra security against spammers or Akismet for anti-spam protection.
Credits and thanks to Contact Form 7 author.
The author of Contact Form 7 is Takayuki Miyoshi, a web developer from Tokyo, Japan. Takayuki has been developing contact forms with WordPress since 2006, when he first released contact-form-7, originally a fork of the popular contact form plugin, which WordPress then adopted as its official contact form plugin. Takayuki’s contact forms have been widely used ever since and are now the most popular contact form plugins for WordPress.
Let’s start with the simplest and probably the most common modification, that is:
How to do something additional (save to a file, save in a database, create an entry, etc.) with the submitted form?
Thanks to the plugin author’s provision, adding such integration is relatively easy. Let’s look at the code below, which implements probably the simplest form of such integration, and at the same time, is an excellent start to building your own more complex versions:
function prefiks_process_submitted_form_data( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
// Get the data submitted in the form
$submitted_name = $submission->get_posted_data('your-name');
$submitted_email = $submission->get_posted_data('your-email');
$submitted_subject = $submission->get_posted_data('your-subject');
$submitted_message = $submission->get_posted_data('your-message');
// ...
// Do something with the data (e.g. save it to a file)
file_put_contents('cf7-log.txt', 'Data: ' . date('c') . "\n", FILE_APPEND);
file_put_contents('cf7-log.txt', "Autor: {$submitted_name} <{$submitted_email}>\n", FILE_APPEND);
file_put_contents('cf7-log.txt', 'Temat: ' . $submitted_subject . "\n", FILE_APPEND);
file_put_contents('cf7-log.txt', "Wiadomość:\n" . $submitted_message, FILE_APPEND);
file_put_contents('cf7-log.txt', "\n--------------------\n", FILE_APPEND);
}
}
add_action( 'wpcf7_before_send_mail', 'prefiks_process_submitted_form_data' );
Additional variants and questions
I have multiple forms. How can I check which one has been sent?
The object of the submitted form is passed as the action parameter. We can check its ID (entry ID with form description), name and title.
$form_id = $contact_form->id();
$form_name = $contact_form->name();
$form_title = $contact_form->title();
My form does not have to send an email after being processed. Can I block it somehow?
Yes. The author of the plugin also foresaw this possibility.in your function attached to the action skip_mail to true as follows:
$ contact_form-> skip_mail = true;
How to get into all the data sent?
Just use $ data = $ submission-> get_posted_data ();. This way, in the $ data we will have an array containing all the data submitted in the form.
How do I process files sent in the form?
For example:
$ uploaded_files = $ submission-> uploaded_files ();
foreach ((array) $ uploaded_files as $ name => $ path ) {
// do something with the file sent from the field named $ name,
// it is on the server in the place specified by $ path
Summary
Thanks to the plugin author’s provision, adding such integration is relatively easy. In addition, thanks to filters and actions, the WordPress plugin Contact Form 7 (CF7) allows for easy expansions. Let’s look at the code below, which implements the simplest form of such integration, and at the same time, is a good starting point for more advanced modifications. If you want to learn How to process data sent with a form Contact Form 7 (CF7), follow our blog post series or contact us for help.
If you have questions about Contact Form 7 or WordPress, don’t hesitate to contact us using email, contact form [CF7 :-)] or on our Linkedin profiles (check About us)
FAQ about Contact Form 7
Contact Form 7 is a free WordPress plugin that allows you to create forms and surveys for your website. It is one of the most popular plugins, offering many features and customization options.
1. Log in to your WordPress dashboard: Visit yourwebsite.com/wp-admin (replace “yourwebsite.com” with your actual domain name) and enter your username and password.
2. Navigate to the Plugins section: On the left sidebar, click “Plugins” and then “Add New.”
3. Search for Contact Form 7: In the search bar at the top right corner, type “Contact Form 7” and press Enter.
4. Install the plugin: Locate the Contact Form 7 plugin by Takayuki Miyoshi in the search results. Click the “Install Now” button to install the plugin.
5. Activate the plugin: Click the “Activate” button to enable the plugin on your website after installation.
6. Access Contact Form 7 settings: Once activated, you will see a new menu item called “Contact” in the left sidebar of your WordPress dashboard. Click on it to access Contact Form 7 settings.