How to integrate a new form plugin with this one

November 25th, 2012 Leave a comment Go to comments

Currently Contact Form 7 and Fast Secure Contact Form are integrated with this plug. That means that submissions from forms made by those plugins will be captured in the database by this plugin automatically.

If you have created a different form plugin then you may want to integrate it with the CFDB plugin so it will save your plugin’s form submission data into the database. This post explains how to do it. Essentially, when your code receives a form submission, you will need to organize the data into a an object structured in the way that CFDB plugin expects, then call a hook to send the data to it.

Calling the Hook

In the code where your plugin handles form submissions, you will need to organize the data into an object (see below). Let’s call that object $cfdb_data. You would then call the “cfdb_submit“ hook:

1
do_action_ref_array( 'cfdb_submit', array( &$cfdb_data ) );

Organizing the Data

You will need to create the $cfdb_data object to send to the CFDB like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$cfdb_data= (object) array(
    // name of the form
   'title' => $form_name,
 
    // $form_data is an associative array of
    // $form_field_name => $form_field_value
    // for each field in the form.
    // For files, the $form_field_value is the file name (no path)
    'posted_data' => $form_data,
 
    // $uploaded_files is an associative array of
    // $form_field_name (same as in $form_data) =>
    // a path on the server where the uploaded file is stored.
    // The CFDB will copy the file into the database.
    // Your plugin can then delete the file if you want.
    'uploaded_files' => $uploaded_files
);

Working Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$formTitleField = 'form_title'; // change this as needed
if (is_array($_POST) && !empty($_POST)) {
    $title = isset($_POST[$formTitleField]) ? 
                   $_POST[$formTitleField] : 'Untitled';
    $posted_data = array();
    $uploaded_files = array();
 
    // Get posted values
    foreach ($_POST as $key => $val) {
        if ($key != $formTitleField) {
            $posted_data[$key] = $val;
        }
    }
 
    // Get uploaded files
    if (is_array($_FILES) && !empty($_FILES)) {
        foreach ($_FILES as $key => $file) {
            if (is_uploaded_file($file['tmp_name'])) {
                $posted_data[$key] = $file['name'];
                $uploaded_files[$key] = $file['tmp_name'];
            }
        }
    }
 
    // Prepare data structure for call to hook
    $data = (object) array(
        'title' => $title,
        'posted_data' => $posted_data,
        'uploaded_files' => $uploaded_files);
 
    // Call hook to submit data
    do_action_ref_array('cfdb_submit', array(&$data));
}

Notes

The function that handles the calls to the “cfdb_submit” hook is CF7DBPlugin::saveFormData($cf7)

 

  1. February 6th, 2012 at 10:40 | #1

    Hi Michael,

    First let me say I love your plugins. I am using them on my site for various pages. However, I am also using two non-CFDB plugins which I would like to enter into the CFDB database. I AM NOT a programmer so the above is Greek to me.

    I would be happy to make a contribution (you determine the amount) if you can add the data from these two plugins to my CFDB database.

    The first plugin is Login with Ajax (http://www.bucketofwombats.com/easy-ftp-upload-for-wordpress/) – It is a widget that handles the login/logout/register process. This is my number one priority of the two plugins.

    The second plugin is Easy FTP Upload (http://netweblogic.com/wordpress/plugins/login-with-ajax/) – It is a secure file upload which uploads to a non-public-html directory (client-uploads) on my server. As this is very sensitive data, I kept it out of the public-html directory on purpose. I am guessing I might have to move the client-uploads directory to public-html in order to have the data in the CFDB database. Maybe we can exempt the sensitive file data and just load the other info from the form to the CFDB database.

    Please let me know if you would be interested in helping me. Also, let me know what amount should I contribute for your time and effort.

    Thanks,

    Rudy

    • Michael Simpson
      February 8th, 2012 at 09:46 | #2

      Integrating other such plugins would likely require having to change code in those plugins (not mine) to send data to my plugin. They you would end up with a customized version of those plugins and those customizations would get wiped out any time you updated those plugins. Not to mention it would be very time consuming for me to figure out another plugin’s code and modify it appropriately.

      I have provided a hook that other plugins can call to push data into mine. I have a post on How to integrate your form plugin with this one. You might approach the authors of those plugins to see if they wish to modify their plugins to call the hook that mine provides.

  2. jasnon
    February 16th, 2014 at 17:50 | #3

    Hi Michael,

    I just recently switched from CF7 over to Gravity Forms and got in contact with their support team to see how I could integrate their plugin with yours. They replied with the following:

    “To take the data that Gravity Forms collects and send it anywhere else, you can use the gform_after_submission hook:

    http://www.gravityhelp.com/documentation/page/Gform_after_submission

    That hook combined with the CFDBcode should be what you need to send the data to CFDB.”

    Based on the article above it appears I need to add that hook along with your code above to my theme’s functions.php, however I’m not that great with code so I can’t seem to figure out how to combine the two pieces to work together. Any chance you could provide some guidance?

    Thanks for your help and support!

  1. No trackbacks yet.
You must be logged in to post a comment.