{"id":747,"date":"2012-06-16T15:08:13","date_gmt":"2012-06-16T19:08:13","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=747"},"modified":"2016-09-21T20:06:34","modified_gmt":"2016-09-22T00:06:34","slug":"filtering-form-data-before-it-is-saved","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=747","title":{"rendered":"Manipulating Form Data Before it is Saved"},"content":{"rendered":"<p>(As of version 2.4)<\/p>\n<p>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. <\/p>\n<p>Before CFDB saves the form data to the database, it gives you an opportunity to modify or &#8220;filter&#8221; it. Do this by registering a a filter in your own plugin or theme PHP code.<\/p>\n<p>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 <a href=\"http:\/\/wordpress.org\/extend\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Add Shortcodes Actions and Filters plugin<\/a> and activate it. From the administration page, go to the <strong>Tools<\/strong> menu -&gt; <strong>Shortcodes Actions and Filters<\/strong>. Create a new code item and put your code there and save. <\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">function myFilter($formData) {\r\n    \/\/ Change\u00a0$formData\r\n    return $formData; \/\/ be sure to return it\r\n}\r\n\r\nadd_filter('cfdb_form_data', 'myFilter');<\/pre>\n<p>$formData is an object with a structure:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">$formData\u00a0= (object) \u00a0array(\r\n    'submit_time' =&gt; 1339365804.7815, \/\/ float Unix timestamp with microseconds\r\n    'ip' =&gt; '192.168.1.1', \/\/ string IP address\r\n    'user' =&gt; 'admin', \/\/ string user name if submitter was logged in. May be null\r\n    'title' =&gt; 'Form name string',\r\n \u00a0\u00a0\u00a0'posted_data' =&gt; array(\/* key=&gt;value for each field in the form *\/), \r\n    'uploaded_files' =&gt; array ('fileupload' =&gt; $_FILES[\"upload1\"][\"tmp_name\"] )) \/\/ may be null\r\n);<\/pre>\n<p>Change values within this data structure and return it.<\/p>\n<p><strong>Example<\/strong><br \/>\nYou have a form with separate field for &#8220;month&#8221;, &#8220;day&#8221;, &#8220;year&#8221; and you want to convert that into a single &#8220;date&#8221; field with format &#8220;month\/day\/year&#8221;.<\/p>\n<p>Using Contact Form 7, create a form like this.<\/p>\n<pre>Month: [text* month] \r\nDay: [text* day] \r\nYear: [text* year] \r\n[submit \"Send\"]<\/pre>\n<p>In this example, I named the form &#8220;<strong>dateform<\/strong>&#8221; and CF7 gave me a short code to put into posts: [contact-form-7 id=&#8221;1323&#8243; title=&#8221;dateform&#8221;].<\/p>\n<p>Add the short code to a post or page. It looks like:<br \/>\n<a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/06\/dateform.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-769\" title=\"dateform\" alt=\"\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/06\/dateform-300x150.png\" width=\"300\" height=\"150\" srcset=\"https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/06\/dateform-300x150.png 300w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/06\/dateform.png 452w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Then go to \u00a0<strong>Tools<\/strong>\u00a0menu -&gt;\u00a0<strong>Shortcodes Actions and Filters<\/strong>\u00a0and add code:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">\/\/ My form filter\r\nfunction myFilter($formData){\r\n    $formName = 'dateform'; \/\/ change this to your form's name\r\n    if ($formData &amp;&amp; $formName == $formData-&gt;title) {\r\n        \/\/ Create a consolidated \"date\" field\r\n        $formData-&gt;posted_data['date'] =\r\n                $formData-&gt;posted_data['month'] . '\/' .\r\n                        $formData-&gt;posted_data['day'] . '\/' .\r\n                        $formData-&gt;posted_data['year'];\r\n\r\n        \/\/ If you like, remove the original fields\r\n        unset($formData-&gt;posted_data['month']); \r\n        unset($formData-&gt;posted_data['day']); \r\n        unset($formData-&gt;posted_data['year']); \r\n    }\r\n    return $formData;\r\n}\r\n\r\nadd_filter('cfdb_form_data', 'myFilter');<\/pre>\n<p><a name=\"debug\"><\/a><strong>Debugging<\/strong><\/p>\n<p>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:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<code>error_log=\/path\/to\/error_log.txt<\/code><br \/>\nAdd calls in your filter function like:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<code>error_log(\"your message\");<\/code><br \/>\nTo print out information to your error log to help you see what is happening, what variable values are, and what code is reached.<br \/>\nTry a submission, then look at the file to see if there are any error messages there.<\/p>\n<p>Example: for debugging purposes, you want to output the $formData data structure to the error log file for each form submission. Add this filter:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">function &amp;log_form_submissions(&amp;$formData)\r\n{\r\n    error_log('Form Submission: ' .\r\n            print_r($formData, true));\r\n    return $formData;\r\n}\r\nadd_filter('cfdb_form_data', 'log_form_submissions');<\/pre>\n<p>If this is not working, you can explicitly specify the file in the code. (Change \/path\/to\/file below).<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">function &amp;log_form_submissions(&amp;$formData)\r\n{\r\n    error_log('Form Submission: ' .\r\n            print_r($formData, true), 3, '\/path\/to\/file.txt');\r\n    return $formData;\r\n}\r\nadd_filter('cfdb_form_data', 'log_form_submissions');<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>(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 &#8220;filter&#8221; it. Do this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":102,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-747","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-c3","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/747","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=747"}],"version-history":[{"count":29,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/747\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/747\/revisions\/1542"}],"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=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}