{"id":646,"date":"2011-12-18T11:43:48","date_gmt":"2011-12-18T16:43:48","guid":{"rendered":"http:\/\/cfdbplugin.com\/?p=646"},"modified":"2016-01-04T21:22:53","modified_gmt":"2016-01-05T02:22:53","slug":"how-to-integrate-your-form-plugin-with-this-one","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=646","title":{"rendered":"How to integrate a new form plugin with this one"},"content":{"rendered":"<p>Currently Contact Form 7, Fast Secure Contact Form and several other form plugins are integrated with this plugin. That means that submissions from forms made by those plugins will be captured in the database by this plugin automatically.<\/p>\n<p>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&#8217;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\u00a0CFDB plugin expects, then call a hook to send the data to it.<\/p>\n<h2>Calling the Hook<\/h2>\n<p>In the code where your plugin handles form submissions, you will need to organize the data into an object (see below). Let&#8217;s call that object <strong>$cfdb_data<\/strong>. You would then call the &#8220;<strong>cfdb_submit<\/strong>&#8221;\u00a0hook:<\/p>\n<pre escaped=\"true\" lang=\"php\" line=\"1\">do_action_ref_array( 'cfdb_submit', array( &amp;$cfdb_data ) );<\/pre>\n<h2>Organizing the Data<\/h2>\n<p>You will need to create the $cfdb_data object to send to the CFDB like this:<\/p>\n<pre escaped=\"true\" lang=\"php\" line=\"1\">$cfdb_data= (object) array(\r\n    \/\/ name of the form\r\n   'title' =&gt; $form_name,\r\n\r\n    \/\/ $form_data is an associative array of\r\n    \/\/ $form_field_name =&gt; $form_field_value\r\n    \/\/ for each field in the form.\r\n    \/\/ For files, the $form_field_value is the file name (no path)\r\n    'posted_data' =&gt; $form_data,\r\n\r\n    \/\/ $uploaded_files is an associative array of\r\n    \/\/ $form_field_name (same as in $form_data) =&gt;\r\n    \/\/ a path on the server where the uploaded file is stored.\r\n    \/\/ The CFDB will copy the file into the database.\r\n    \/\/ Your plugin can then delete the file if you want.\r\n    'uploaded_files' =&gt; $uploaded_files\r\n);<\/pre>\n<h2>Working Example Code<\/h2>\n<pre escaped=\"true\" lang=\"php\" line=\"1\">$formTitleField = 'form_title'; \/\/ change this as needed\r\nif (is_array($_POST) &amp;&amp; !empty($_POST)) {\r\n    $title = isset($_POST[$formTitleField]) ? \r\n                   $_POST[$formTitleField] : 'Untitled';\r\n    $posted_data = array();\r\n    $uploaded_files = array();\r\n\r\n    \/\/ Get posted values\r\n    foreach ($_POST as $key =&gt; $val) {\r\n        if ($key != $formTitleField) {\r\n            $posted_data[$key] = $val;\r\n        }\r\n    }\r\n\r\n    \/\/ Get uploaded files\r\n    if (is_array($_FILES) &amp;&amp; !empty($_FILES)) {\r\n        foreach ($_FILES as $key =&gt; $file) {\r\n            if (is_uploaded_file($file['tmp_name'])) {\r\n                $posted_data[$key] = $file['name'];\r\n                $uploaded_files[$key] = $file['tmp_name'];\r\n            }\r\n        }\r\n    }\r\n\r\n    \/\/ Prepare data structure for call to hook\r\n    $data = (object) array(\r\n        'title' =&gt; $title,\r\n        'posted_data' =&gt; $posted_data,\r\n        'uploaded_files' =&gt; $uploaded_files);\r\n\r\n    \/\/ Call hook to submit data\r\n    do_action_ref_array('cfdb_submit', array(&amp;$data));\r\n}\r\n<\/pre>\n<h2>Notes<\/h2>\n<p>The function that handles the calls to the &#8220;cfdb_submit&#8221;\u00a0hook is <strong>CF7DBPlugin::saveFormData($cf7)<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently Contact Form 7, Fast Secure Contact Form and several other form plugins are integrated with this plugin. 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":102,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-646","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-aq","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=646"}],"version-history":[{"count":20,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/646\/revisions"}],"predecessor-version":[{"id":1356,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/646\/revisions\/1356"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/102"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}