Gravity Forms is a flexible, easy to use form plugin for WordPress. The vast list of hooks and filters allows the plugin to extended in almost any way possible.
Recently I had a client who wanted data submitted in a form to also be added to a custom database table. Gravity Forms made this task easy to accomplish.
1. Create Custom Database Table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
register_activation_hook( __FILE__, 'endo_create_custom_table' ); | |
function endo_create_custom_table() { | |
global $wpdb; | |
$table_name = $wpdb->prefix . "custom_table"; | |
$sql = "CREATE TABLE $table_name( | |
id mediumint(9) NOT NULL AUTO_INCREMENT, | |
entry_id VARCHAR(50) NOT NULL, | |
date datetime, | |
email VARCHAR(50) CHARACTER SET utf8, | |
param1 VARCHAR(50) CHARACTER SET utf8, | |
param2 mediumint(1) DEFAULT 0, | |
UNIQUE KEY id(id) | |
) COLLATE utf8_general_ci;"; | |
require_once(ABSPATH . '/wp-admin/includes/upgrade.php'); | |
dbDelta($sql); | |
} |
First, we create a function to run on plugin activation, endo_create_custom_table
. By setting the function to run on plugin activation the custom table function will only run once. Inside of the function set the name of the custom table. You can prepend the default table prefix for the particular WordPress installation using $wpdb->prefix
.
Next, set a variable $sql
with the information for the new table. In this case we are adding a column for id, entry_id, date, email, param1, and param2. These columns can be set to anything you like. In this case we need a place to store information from two unique fields of the form (param1 and param2), plus the date, entry_id of the form entry, and the email address submitted through the form.
Finally, pass the variable to the dbDelta()
function. This function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary.
Learn more about creating tables in a WordPress plugin.
2. Use Gravity Forms Hook to Save Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('gform_after_submission', 'endo_add_entry_to_db', 10, 2); | |
function endo_add_entry_to_db($entry, $form) { | |
// uncomment to see the entry object | |
// echo '<pre>'; | |
// var_dump($entry); | |
// echo '</pre>'; | |
$source = $entry['source_url']; | |
$email = $entry[5]; | |
$param1 = $entry[2]; | |
$param2 = $entry[3]; | |
global $wpdb; | |
// add form data to custom database table | |
$wpdb->insert( | |
'custom_table_name', | |
array( | |
'source_url' => $source, | |
'email' => $email, | |
'param1' => $param1, | |
'param2' => $param2, | |
'date' => current_time( 'mysql' ) | |
) | |
); | |
} |
Using the gform_after_submission
hook, we can run a function after a form is submitted. In this case we want to save some of the data to our custom database table from step 1.
First, create a function and pass the $entry
and $form
object to it so the form data can be used in the function. Next, determine which entry value to use for each piece of data you need to save. An easy way to find the correct id is to View Source on the form and find the id of the form input you want.
For example, if the id of the email input of your form is input_1_5, then the email address can be accessed with $entry[5]
.
Using the insert
method of the $wpdb object, now we take our submitted data and save it to our custom database table. Enter the name of the table, and then enter an array of values to save to table. Make sure the column name matches exactly, or none of your data will be saved.
If saving the data fails, the function won’t tell you which value caused the error. Test it with one value, and once that value saves, move on to the next one. Continue until all of the values save correctly.
Conclusion
Saving data into a custom database table in WordPress can be done easily with Gravity Forms. Using the hooks and filters supplied with Gravity Forms, you can manipulate and use the data any way you like.
What a great idea, thanks so much for sharing this! I tried it with one form and it works great…could you use the code that saves to a different table for let’s say only two forms?
below, I wrote how I have mine for one form…. and I would love to see if I could get it to do the same thing for form number 12.
add_action(‘gform_after_submission_9’, ‘endo_add_entry_to_db’, 10, 2);
function endo_add_entry_to_db($entry, $form) {
thanks again so much for sharing and hope I can thank you again 🙂
G.
Hi, thanks for the great tutorial.
But I can’t seem to get it to create the table.
I have added both parts of your code to my themes function.php
cheers rich
Is it possible to store different parameter entry to different data table from a single Gravity Form.
Hi, where have i to put the code? In which files? function.php?
Both parts? Thanks
Hi Davide,
You can put it in your functions.php file, or in your plugin file.
This hack supports the advanced field: “List field with multiple columns”? That is, I have a form, with only one field (field list with multiple columns option active) and I created 4 columns (1 text, 1 date, 1 numbers 1 and file upload), I wish that each row of each column was displayed in a corresponding column of a table. Can be done? I hope I made it clear. Here’s an example of what I would do -> http://imgur.com/a/PJnK8
I write here after a relentless search on google, and as I tried to use “gravity view” but does not support the field list (with multiple columns) I’m trying to shift attention to mysql and refer to if you can.
Thank you in advance, Fabio
Thank you very much, I have solved a major problem with this script.
But .. what if the form is updated?
The problem is that, by duplicating the data, the tables do not remain aligned if the main module data is changed or deleted.
Which GF hook I could use to update the custom table?
Thanks again
This was awesome, thank you so much!
Hello,
ihave paste this code in the function.php file in my theme but the table isn’t create
Hey Jeremy. Just wanted to say thank you for making this tutorial so simple and easy to understand. I was able to use your code to create my own function to save data into a custom table.
Thanks dude!
Sure thing Jim! Glad it was helpful.
Hello!
I really don’t know why this isn’t working :(. Could you help me ? I’m sure its a easy fix.
Do you have an email or something to contacto you ? 🙂
Thanks so much. Client needed data written to a file, and with your sample code, it was cake. Greatly appreciated.
May i know the file name inside the gravity form plugin. because i have added the gform submission code in “gravityforms.php” but it is not save the data into custom table. Please guide me
Awesome post, Jeremy. Thanks for sharing!
Any Video Tutorial on how it works, step by step?
HI,
How would it be possible to get different forms save datas in different tables?
form_1 saves datas in table_a
form2_ saves datas in table_b
…
Thanks,
Michael
Thank you very much for the quick example!
I wasn’t sure how to select which form ID until I looked at the Gravity Forms documentation link you provided.
You’re welcome!
Does this still save an entry to the wp_posts table like it usually does or does this method replace that entirely?
It still saves an entry under Gravity Forms > Entries.