Wordpress:ContactForm7 - Save Submitted Data To Your Own Database

Published on
3,196 Points
Last Modified:
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.

I recently worked on a Wordpress site that utilized the popular ContactForm7 plug-in that was paired with Flamingo to save data to the WordPress Admin panel.

If you are happy with submitted data going directly to Wordpress where you can export manually, you're good at this point if you just install Flamingo. However, my task was to integrate submitted data to a custom CRM database.  

I did some searching and found there are others looking to do the same. Unfortunately, Google points to some solutions at StackOverflow which all seemed to be downvoted or closed as off-topic. 

Need to do redirections in functions.php and Contact Form 7 get_posted_data(); is not returning posted values are a couple of examples. Hopefully, others will find this article useful and can create their own questions based on the information here.

My Solution

I am using a child theme that contains both functions.php and style.css in the theme-child folder. Some of the big all in one themes generate their own child theme and as an extra precaution for files being updated, I create a new folder inside my WordPress folder called CustomFunctions. Inside that folder I have two files for custom.config.php and custom.functions.php

My ContactForm7 has the following code

<label> Your Name (required)
[text* your-name] </label>
<label> Your Email (required)
[email* your-email] </label>

<label> Your Message
[textarea your-message] </label>

[recaptcha size:compact]

[submit "Send"]

If you were to view the source of the rendered fields, it would look something like

<input type="text" name="your-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false">

You can see that  [text* your-name] equates to the field name =”your-name". The field names I will capture will be “your-name”, “your-email” and “your-message”. In addition, ContactForm7 is capturing the IP, the URL from where the form is submitted and the user agent (browser information) which I am also going to capture.

First thing I have is my own custom config file that will contain my database credentials and anything else I might need to set up.




// **************** database *************

$db_host = "localhost";

$db_name = "the_db_name";

$db_user = "db_user_name";

$db_word = "db_password";

Next is the function that will save data to our database as well as anything else you may want to do. For simplicity, I am saving to the database only.



include_once 'custom.config.php';

if (!$dbCustom = new PDO("sqlsrv:Server=localhost;Database=$db_name", $db_user , $db_word))


die("0,There is a problem, please call the administrator");


function addLead($name,$email,$comment,$remoteIP,$url,$userAgent){

global $dbCustom;

$stmt = $dbCustom->prepare('INSERT INTO dbo.tWebLeads (name,email,Comment,RemoteIP,

URL, UserAgent)

VALUES(:your-name, :your-email,:your-message,:remoteip,:url,:userAgent)');

$stmt->bindValue(':your-name', $name, PDO::PARAM_STR);

$stmt->bindValue(':your-email',  $email,  PDO::PARAM_STR);

$stmt->bindValue(':your-message',  $comment,  PDO::PARAM_STR);

$stmt->bindValue(':remoteip',  $remoteIP,  PDO::PARAM_STR);

$stmt->bindValue(':url',  $url,  PDO::PARAM_STR);

$stmt->bindValue(':userAgent',  $userAgent,  PDO::PARAM_STR);


//print_r($dbCustom->errorInfo());  //TESTING ONLY

$stmt = NULL;

Last, in the child theme functions.php I am capturing the data and sending to my addLead function.

Child theme functions.php


include_once $_SERVER["DOCUMENT_ROOT"].'/customfunctions/custom.functions.php';

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

function my_theme_enqueue_styles() {

wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );


add_action('wpcf7_before_send_mail', 'save_form' );

function save_form( $wpcf7 ) {

global $dbCustom;

$submission = WPCF7_Submission::get_instance();

if ( $submission ) {

// If you want to see all the values of $submission, you can add the two lines below. Just make sure you remove for production. To view the result, you will have to use your browser console network tab as this is an ajax request.

// print_r($submission);


$submited = array();

$submited['posted_data'] = $submission->get_posted_data();


$name = $submited['posted_data']['your-name'];

$email = $submited['posted_data']['your-email'];

$comment = $submited['posted_data']['your-message'];

$remoteIP = $submission->get_meta('remote_ip');

$url = $submission->get_meta('url');  // Helpful when you have the same form on multiple pages

$userAgent = $submission->get_meta('user_agent');

// call addLead function from the custom.functions.php


// send our own custom message to ourself

wp_mail( 'you@example.com', 'new contact', 'there is an new contact in your database');


Please let me know your comments below.


Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Join & Write a Comment

Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month