November 1, 2024
Blogger working on their Wordpress site

If you are familiar with WordPress, you are aware of their plugins, which is a wonderful system that provides countless ways to add the features you want to your website.  And the amazing part is a large number of these plugins come at no cost at all.  

But even with the enormous number of free plugins, they often have the knack of holding back an important concluding feature behind a paywall. It is a completely understandable model, and I and many others greatly appreciate the free features they do provide, but it is something to be aware of when first stepping into the massive world of WordPress plugins.

A prime example, and one that almost all new websites run into is Contact Forms. As they work well out of the box to address the early steps of the issue, but in terms of finishing the job and having your submission information placed in your own site’s database, they fall a bit short. But fear not, as we are going to bring to you some zero cost options that are available to you to finish this job.

Below, we will be discussing the general features that normally come with free Contact form plugins, the methods they use to provide you with the submission data, and then cover a couple of different approaches that can provide you with some better alternatives by saving your submission data.

What features are normally available with free Contact forms?

The majority of Contact Form plugins that are free to download and use provide the following features:

  • Data collection template form
  • Email delivery method of the form’s submission information

The template forms provided are almost always fine, especially for generic usage and contain the main requirements for name, email and message fields. No real problem here. The issue with most free plugins is their method of information delivery. Once a Contact form is submitted on your website, the plugins rely on sending the information to you through email.

This relies on you properly setting up multiple email addresses, one for sending the email and one for receiving the email and is a crucial step for configuring the plugin before you start using it. And the setup of the sender email address is required to be on the same domain as your website or the entire process doesn’t work. Even with a straightforward setup, you have to hope that no communication error or hiccup occurs along the way for any submission.

That doesn’t sound like a very reassuring business plan for something as important as potential contacts and leads (and this applies to any type of submission form, not just Contact forms). While this system might work, it makes sense this is one of the top-rated searches surrounding WordPress plugins, as users want something a bit more reliable. And reliable means to be able to write your Contact form submission information directly to your WordPress database, in real-time.

What are the best ways to record submission data directly to your database?

There are a couple of solutions available to move from an email to a direct database delivery method for submission forms. Both of these new data delivery methods should allow you to place your information in your local database and do it at no extra cost.

Install another plugin that adds this functionality to your existing Contact submission forms plugin for free.

The plugin world of WordPress is nothing if not diverse, and a number of plugins work together to provide synergy between feature sets, and some unlock secondary or advanced features that work with other plugin’s base functions.

There are a number of plugins available that provide this secondary feature if you have a primary Contact form submission plugin already installed. Below are some intriguing add-ons that add direct database storage functionality to already installed form plugins. Please note that many of these add-ons are made specifically to work for one or two primary plugins.

Tablesome WordPress plugin

Tablesome is a Contact Form database add-on plugin to save Contact Form submissions to tables in WordPress. Integrates with WPForms and Contact Form 7 to save entries to your database for free. Also includes features for redirection after form submit, import and export tables & entries as CSV & XLSX files, and automatically export data to Mailchimp, Notion, etc.

Flamingo WordPress Plugin

Flamingo is a message storage plugin originally created for Contact Form 7 (by the creators of Contact Form 7), which does the job of storing submission form data into your local WordPress database. Once installed it will automatically begin storing submission data locally in the form of posts.

Contact Form 7 Database Addon “CFDB7” WordPress Plugin

Contact Form 7 Database Addon, the “CFDB7” plugin saves contact form 7 submissions to your WordPress database and works much like the above-mentioned Flamingo, works automatically once installed and enabled, and is adequate for the job.

Database Addon For WPForms (WPFormsDB) WordPress Plugin

Database Addon For WPForms the “WPFormsDB” plugin saves WPForms entries to your WordPress database. Export the data to a csv file. By simply installing the plugin, it will automatically begin to capture form submissions from WPForms.

Before moving on to the next method, I wanted to mention another free plugin option that provides Contact form submission and doesn’t use email to transmit the information.

JotForm Online Forms WordPress Plugin

Jotform Online Forms is an easy-to-use form builder plugin that offers a free version but provides a compromise to using email or directly submitting into your local database.  With submissions, the information is submitted directly to a Jotform server where it can be accessed by you through an account.  A decent approach without the worry of email and is free if other options fail.

The second method is taking the solution into your own hands and updating your WordPress site to save this information through some PHP code.

This is the “do-it-yourself” method and will be a 2-step setup process; you also want to ensure you already have a Contact or Form builder plugin installed and working on your site before moving forward.

The first step involves setting up a new table in your local MySQL database that will receive and store your form submission data.  The second step covers the necessary PHP code that will facilitate the saving of your form data to your local database and the table we setup in the previous step.

Please note that you will need to be able to access your Hosting server, whether physically or through remote login, to be able to add the new database table and update any necessary permissions.

MySQL Database Setup:

You will need to access your MySQL database, and the easiest method is using your phpMyAdmin utility.  If you are working on a localhost server, you can type the following directly into the URL address bar:

  • http://localhost/phpmyadmin/
  • http://127.0.0.1/phpmyadmin/ (in most setups you can also just use the localhost IP)

If your server is hosted, you likely have a control panel with access to phpMyAdmin.  Using Bluehost as an example you can gain quick and easy access to this utility using their cPanel:

mySQL myphpAdmin  Tool

After logging in with your database admin user and password, you will be placed on the main phpMyAdmin screen which will list your available databases.  Click on your site’s MySQL database to select (it should expand and load its tables in the main screen).

MySQL Database

Once within the database, do the following 3 steps.  Click Operations, then enter in your new table’s name, enter the number of fields and finish by hitting Create.

The number of fields should equal the same number of distinct data elements you are collecting on your Form.  And do not worry if you forgot a field in this step, you could always add additional fields in the next step, if needed.

Here you will need to define your data fields, by entering in its Name and data Type.  For any character field you will also need to specify a field Length and for the ID field you want to make sure its Index is set to PRIMARY.  

In this example, we are creating the ID, Name, Email, Subject, Message and a Submitdate data fields in our [wp_contact_feedback] table.

The ID field will be the table’s primary key and will auto-generate as new records are added.  Once you identify it as the primary key you will get a pop-up window, everything should be completed as needed and you can just hit Go.

Once your data fields are entered and defined, make sure the database the Storage Engine is set to InnoDB and hit the Save button.

MySQL save new data fields

That completes the MySQL database setup.

Adding PHP code to WordPress site:

The general function used to retrieve the submitted form data and write it to the new database is pretty much the same in terms of MySQL connection and inserting the data across all plugins.  However, there are two parts of the function that will change depending on whose Form(s) you are using, as each vendor has slightly different naming conventions and slightly different methods for storing the data.

We have 2 examples below, one for Contact Form 7 and a second for WPForms (Lite), that displays code to write the data from those forms to your database.  Other Contact / Form builder plugins will work similarly, but you would need to find the proper “hook” function, as well as the proper method to grab the submitted data from their form object.  

With these examples, before they can be used with your site, you will need to make some adjustments to a few parameters to ensure they match up with your system’s configuration and data setup. 

The first section, highlighted in yellow at the top of each snippet, is related to your MySQL login and database information – this will allow you to connect with your database from your site. The second section, highlighted in green and orange near the bottom of each snippet, is related to writing the submitted data to your local database. (Copyable code is available at the end of the article.)

ContactForm7_connection_logic

Each of the yellow parameters needs to be changed to fit permissions for your site. These are same login credentials for getting into myPhpAdmin (not for WordPress). And in many cases the $userhost should remain as ‘localhost’ – even for sites that are live. Check with your hosting platform if you want to be certain, as rules vary by vendor.

Contact Form7 insert logic

The green parameter needs to be updated to be the name of the table your submission data is going to be stored in – which should be the table we just created in the earlier step. And the orange parameters need to be updated to match the naming convention of the data fields of your table. In order they are the Name, Email, Subject and then Message fields. The ID field is an auto-generating on and no input is needed.

Below is the logic if you are using WPForms (lite). And while the parameter configuration is exactly the same, the method of pulling the submitted data is a bit different.

WPForms_connection
WPForms insert logic

Once you have your appropriate Code snippet updated with your correct parameters, we can move onto the final step. Placing this code snippet into your site. There are two ways you can go.

Adding the snippet manually

You can add this code snippet manually to your theme’s functions.php file.   It can be located in the following directory and can be edited with notepad or a text editor like Notepad++.  

  • [your-site]\wp-content\themes\[your-theme]\functions.php

The built-in Theme File Editor is another method that can also be used to manually add the snippet, which is located within your Admin Dashboard, under Appearance -> Theme File Editor. Once loading your theme files, you can click on the functions.php file under Theme Files on the right-hand side of the screen. You can then take the code snippet above, after you have made your edits, and paste it into the bottom of the file. Hitting Update File will save it to our site.

Theme File Editor

Please note that making changes directly to the functions.php does have some risk and it is usually not recommended, especially if you are still a novice.  The next option is the recommended method.

Adding the snippet using a plugin

The WPCode plugin has a free version available that allows you to save a snippet of Php code to your site with little effort.  The plugin will handle making sure it is placed in the proper place, plus it has the benefit of not being overwritten if your theme has an update.

You can install the plugin by going to your Admin Dashboard -> Plugins -> Add New and entering in WPCode in the search.

WPCode_install

Upon activation, the plugin will add a new menu item labeled ‘Code Snippets’ to your WordPress admin bar. Clicking on it will show you a list of all the custom code snippets you have saved on your site.

Since you just installed the plugin, your list will be empty. Go ahead and click on the ‘Add New’ button to add your first custom code snippet in WordPress.

WPCode_add_new

This will bring you to the ‘Add Snippet’ page. Here you can choose a code snippet from the pre-made library or add your custom code. To add custom code, click on the ‘Use snippet’ button underneath the ‘Add Your Custom Code (New Snippet)’ option.

W:PCode_use_snippet

You need to start by entering a title for your custom code snippet. After that, you can copy and paste your code snippet into the code box. Be sure to select PHP Snippet from drop-down menu on the right.

WPCode_php_snippet

Next, check to make sure the Insertion method is where we want it. You want it to be set to Auto Insert and the location to be Run Everywhere.

WPCode_run_anywhere

When you’re finished with everything, toggle the switch from ‘Inactive’ to ‘Active’ in the top right corner and then click the ‘Save Snippet’ button.

WPCode snippet active

That should complete both of the steps needed to have your Contact submissions begin writing to your local database. You will want to run some tests to make sure it is working as intended. Entering a Contact submission and then checking your database table through your myPhpAdmin tool should provide the confirmation needed.

I hope you found this information helpful. If anyone has any questions or issues with this process, please add a comment to the post and I will do my best to help out.

In the near future, I plan to release a write-up on the steps to creating an Administrative Menu Option that pulls in your Contact submissions directly data in your site’s interface. That way you can review your form submissions in our Admin screen and not have to keep going to your database to retrieve this data. I hope to have that available soon.

==============================
Save Submission data using Contact Form 7
==============================
// write hook statement
add_action(‘wpcf7_before_send_mail’, ‘contact_form7_save_before_mail’, 10, 1);
 
// writes contact form data to MySQL WP database
function contact_form7_save_before_mail($contact_form) {
    //database connection information
    $username = ‘root’;
    $userpass = ”;
    $userdatabase = ‘curiositywave’;
    $userhost = ‘localhost’;
    // create connection
    $mydb = new wpdb($username, $userpass, $userdatabase, $userhost);
    // retrieve data from form
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
        $submission_data = $submission->get_posted_data();
        $save_name = $submission_data[‘your-name’];
        $save_email = $submission_data[‘your-email’];
        $save_subject = $submission_data[‘your-subject’];
        $save_message = $submission_data[‘your-message’];
        $save_date = date(‘Y-m-d H:i:s’);
    }
 
// write submission data
$mydb->insert(‘wp_contact_submissions’, array(‘id‘=>null,’name‘=>$save_name,’email‘=>$save_email,’subject‘=>$save_subject,’message‘=>$save_message,’submitdate‘=>$save_date), array(null,’%s’,’%s’,’%s’,’%s’,’%s’) );
}
===============================
Save Contact submissions using WPForms
==============================
// write hook statement
add_action(‘wpforms_process_entry_save’, ‘contactform_save_before_mail’, 10, 3);
 
// writes contact form data to MySQL WP database
function contactform_save_before_mail( $fields, $entry, $form_id ) {
    //database connection information
    $username = ‘root’;
    $userpass = ”;
    $userdatabase = ‘curiositywave’;
    $userhost = ‘localhost’;
    $mydb = new wpdb($username,$userpass,$userdatabase,$userhost);
    // retrieve submitted Form data
    if($fields) {
    $form_data = array();
    foreach ($fields as $key => $d) {
        $d[‘value’] = is_array( $d[‘value’] ) ? implode(‘,’, $d[‘value’]) : $d[‘value’];
        $form_data[$key] = $d[‘value’];
    }
    $save_name = $form_data[0];
    $save_email = $form_data[1];
    $save_subject = $form_data[3];
    $save_message = $form_data[2];
    $save_date = date(‘Y-m-d H:i:s’);
    }
// write data to database
    if($form_data) {
    $mydb->insert(‘wp_contact_submissions’,          array(‘id‘=>null,’name‘=>$save_name,’email‘=>$save_email,’subject‘=>$save_subject,’message‘=>$save_message,’submitdate‘=>$save_date), array(null,’%s’,’%s’,’%s’,’%s’,’%s’) );
    }
}