Manipulating Form Data Before it is Saved

September 21st, 2016

(As of version 2.4)

Add, change or remove fields from form submissions before they get saved to the database. Or do other actions with the data such as save it to some other destination.

Before CFDB saves the form data to the database, it gives you an opportunity to modify or “filter” it. Do this by registering a a filter in your own plugin or theme PHP code.

WordPress does not give you a good place to put this code. So I wrote a plugin to give you a good place for it. Install the Add Shortcodes Actions and Filters plugin and activate it. From the administration page, go to the Tools menu -> Shortcodes Actions and Filters. Create a new code item and put your code there and save.

1
2
3
4
5
6
function myFilter($formData) {
    // Change $formData
    return $formData; // be sure to return it
}
 
add_filter('cfdb_form_data', 'myFilter');

$formData is an object with a structure:

1
2
3
4
5
6
7
8
$formData = (object)  array(
    'submit_time' => 1339365804.7815, // float Unix timestamp with microseconds
    'ip' => '192.168.1.1', // string IP address
    'user' => 'admin', // string user name if submitter was logged in. May be null
    'title' => 'Form name string',
    'posted_data' => array(/* key=>value for each field in the form */), 
    'uploaded_files' => array ('fileupload' => $_FILES["upload1"]["tmp_name"] )) // may be null
);

Change values within this data structure and return it.

Example
You have a form with separate field for “month”, “day”, “year” and you want to convert that into a single “date” field with format “month/day/year”.

Using Contact Form 7, create a form like this.

Month: [text* month] 
Day: [text* day] 
Year: [text* year] 
[submit "Send"]

In this example, I named the form “dateform” and CF7 gave me a short code to put into posts: [contact-form-7 id=”1323″ title=”dateform”].

Add the short code to a post or page. It looks like:

Then go to  Tools menu -> Shortcodes Actions and Filters and add code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// My form filter
function myFilter($formData){
    $formName = 'dateform'; // change this to your form's name
    if ($formData && $formName == $formData->title) {
        // Create a consolidated "date" field
        $formData->posted_data['date'] =
                $formData->posted_data['month'] . '/' .
                        $formData->posted_data['day'] . '/' .
                        $formData->posted_data['year'];
 
        // If you like, remove the original fields
        unset($formData->posted_data['month']); 
        unset($formData->posted_data['day']); 
        unset($formData->posted_data['year']); 
    }
    return $formData;
}
 
add_filter('cfdb_form_data', 'myFilter');

Debugging

To help debugging, define an error log file where PHP can print error messages. Create a wp-content/plugins/contact-form-7-to-database-extension/php.ini file with a line that points to where a log files should be written:
    error_log=/path/to/error_log.txt
Add calls in your filter function like:
    error_log("your message");
To print out information to your error log to help you see what is happening, what variable values are, and what code is reached.
Try a submission, then look at the file to see if there are any error messages there.

Example: for debugging purposes, you want to output the $formData data structure to the error log file for each form submission. Add this filter:

1
2
3
4
5
6
7
function &log_form_submissions(&$formData)
{
    error_log('Form Submission: ' .
            print_r($formData, true));
    return $formData;
}
add_filter('cfdb_form_data', 'log_form_submissions');

If this is not working, you can explicitly specify the file in the code. (Change /path/to/file below).

1
2
3
4
5
6
7
function &log_form_submissions(&$formData)
{
    error_log('Form Submission: ' .
            print_r($formData, true), 3, '/path/to/file.txt');
    return $formData;
}
add_filter('cfdb_form_data', 'log_form_submissions');
  1. dam
    September 21st, 2013 at 10:45 | #1

    Can I use this tecnique to save the URL of page/post where is the form submission?

    thanks

    • Michael Simpson
      September 21st, 2013 at 11:19 | #2

      Yes you could do something like:

      $formData->posted_data['uri'] = $_SERVER['REQUEST_URI'];
      or
      $formData->posted_data['url'] = "http://".$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

      …but that is the URL where the form gets posted, not where the form is (unless they are the same). If they are different, you would have to do some javascript trick to create a form field that grabs the URL of the browser and posts it.

  2. JAHD
    September 24th, 2013 at 14:19 | #3

    Hi Michael, CFDB is a great plugin and I’ve even donated to it but I need clarity after searching the articles on this website:

    What is the very most *basic* code to get the chosen value from a drop-down menu (we’ll call it “options” in this example) so that I can use it in my own function?

    • Michael Simpson
      September 24th, 2013 at 18:03 | #4

      It doesn’t matter if it is a drop down menu that supplies the field value. The drop down will have a field name (say “options”) and in a filter function you can access the value chosen by using:
      $formData->posted_data['options']

  3. esyalaa
    September 7th, 2014 at 22:26 | #5

    Hi.
    Can this techniques shows the url of the file that i uploaded to the database?

    For example,
    http://www.your-site.com/wordpress/wp-content/uploads/wpcf7_uploads/video.mp4

    Thanks 🙂

Comments are closed.  Go To Support Forum