Save Gravity Forms Data to a Custom Database Table

Gravity Forms Field Inputs

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

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

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.

Get help customizing Gravity Forms

17 thoughts on “Save Gravity Forms Data to a Custom Database Table

  1. 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.

  2. 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

  3. 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

  4. 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

  5. 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!

  6. 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 ? 🙂

  7. 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

  8. 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

Leave a Reply

Your email address will not be published. Required fields are marked *