I am really interested in trying to successfully store contact information in a table inside the WP database, but I’ve been reading a lot and it is not that simple (at least, for a php beginner like myself). To be honest, given the number of people that have asked about this in the forum, I am surprised that this feature has not yet been incorporated in the plugin and that, AFAIK, there isn’t an “official” solution (post, doc, article or whatever) to do this.
About the “solution” posted by svoinea, obviously it would not work as is because what that code does is gather some of the posted data and send it to a custom function (which, I guess, must do the DB inserting itself at some point).
Now, based on what I’ve been reading, the easiest and simplest way to store CF7 data would be by retrieving the $POST array, but in order to do that, you would need to reference the form’s name, which (according to Firebug) is not defined in the html code. What IS defined, however, is the form’s id, which (obviously) varies from one cf7 form to the other and can be retrieved from the source html code.
Putting together what kevcpu asked about multiple forms and converting2wp‘s contributions regarding how to retrieve form ids, I’m thinking that the best way to do this would be to do the following through a function (inside functions.php, custom_functions.php, user_functions.php or similar method):
1. Get the form’s ID as a value and store it under a $formid variable;
2. Resort to an if statement which would be based on £formid’s value:
if($formid == id_of_form_1 ) {
RETRIEVE DATA FROM FORM_1;
STORE FORM_1 DATA IN TABLE_1;
} elseif($formid== id_of_form_2){
RETRIEVE DATA FROM FORM_2;
STORE FORM_2 DATA IN TABLE_2;
}
Please note that this is not a working example: only a logical outline.
3. Add the function to the cf7 hook mentioned earlier:
add_action( 'wpcf7_before_send_mail', 'my_function' );
– Table_1 and table_2 would have to be already created with all the columns and set field data types necessary to store the data.
– To retrieve data from forms, you could use the code posted by svoinea as an example and adapt or expand it based on your needs.
– To store the data inside the DB tables, there are a lot of tutorials and Codex articles that tell you how to do it, such as this one or this one.
– As for the form ID, converting2wp had some ideas about retrieving that info, although I’m not sure if it should be:
$formid = $cf7->id["_wpcf7"];
or plain:
$formid = $cf7->id;
Perhaps he/she could shed some light on this.
That would be the overall logic to it. What is useful about this function is that it would handle multiple forms and it’s scalable (you can add more forms and include them through additional elseif steps). If you have A LOT of forms, you could work with a “switch” statement instead of an if/elseif on (you would have to code less that way). In my case, I’m only going to have 3 forms tops, so if/elseif makes more sense to me. I’m still trying to figure out the actual code, but perhaps if we put our brains together we might figure it out faster.
Hope this helps. Let me know if this makes sense and what you think about it. Cheers!