Submitting Data via PHP
Perhaps you wish to save data from a form of your own, not CF7 or FSCF. Or perhaps you just want to programatically push data into the DB. To do this there are two basic things you will need to know:
- How to struture your form data so that the plugin knows how to consume it
- How to call the plugin’s save data function
Structuring the data
Data should be structured like this:
$data = (object) array( 'title' => 'form-name', 'posted_data' => array( 'fname' => $_POST['fname'], 'lname' => $_POST['lname'], 'email' => $_POST['email']), 'uploaded_files' => null);
Where ‘form-name’ is the name of the form, and ‘fname’, ‘lname’, and ’email’ are form fields in this example. Replace them with the fields from your form.
‘uploaded-files’ (if used) is an associative array of field_name => pathnames on the local drive to files that are uploaded. For example:
uploaded_files => array ( 'field-name' => '/path/to/the/filename' )
When there is a file upload, the information about it appears twice.
- First, it appears in the list of fields (where you see fname, lname, etc. above) as ‘field-name’ => ‘upload-filename’
- Second it appears inside the uploaded_files associative array as ‘field-name’ => ‘path-to-file’ (use the same ‘field-name’ in both).
Files uploaded from a form post get saved to a temporary disk location. You can find this using the PHP $_FILES variable. Here is an example:
$data = (object) array( 'title' => 'form-name', 'posted_data' => array( 'upload1' => $_FILES['upload1']['name'], 'email' => $_POST['email']),'uploaded_files' => array ('fileupload' => $_FILES["upload1"]["tmp_name"] ));
Delete When Done
When done processing an uploaded file then delete (unlink) the files when done.
Calling the Plugin
Directly use the Plugin
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php'); $plugin = new CF7DBPlugin(); $plugin->saveFormData(&$data);
Or Use a hook
Both CF7 and FSCF send form submission data to hooks that this plugin subscribes to. You can simply publish your form submission to one of those hooks. The advantage is that you do not need to include the CF7DBPlugin.php file, it is a de-coupled approach. The disadvantage is that any other plugin listening to the hook will also get the data (but maybe you want that…)
You can use either hook. You don’t need to have these other plugin’s installed. A hook is just a name.
Use CF7’s Hook
do_action_ref_array( 'wpcf7_before_send_mail', array( &$data) );
Or Use FSCF’s Hook
To Do: We need to have CFDBPlugin have its own hook to listen to.
do_action_ref_array( 'fsctf_mail_sent', array( &$data ) );
On writing this I realize that the plugin should have its own hook so that people can use that decoupled approach without needing to worry about other hook subscribers. As of writing the current version is 1.8.3. I will put the following hook in version 1.8.4.
do_action_ref_array( 'cfdb_submit', array( &$data ) );