WordPress.org

Ready to get started?Download WordPress

Forums

[closed] [Plugin: Contact Form 7] Insert contact name and email in database after successful submission (21 posts)

  1. svoinea
    Member
    Posted 4 years ago #

    Hello,

    I would like to execute a function that inserts into a database the name and the email that were submitted using CF7.

    Is there a way to define custom functions that are executed on successful form submission?

    I know I can hack the source code to achieve this but it will break everything upon plugin upgrade.

    So, is there a simple and upgrade-friendly way to do this?

    Any tips are more than welcome.

    Thank you!

    http://wordpress.org/extend/plugins/contact-form-7/

  2. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    Yes, Contact Form 7 has many action/filter hooks in its code. So, if you are familiar with WordPress plugin development, you can use them to save data into DB. wpcf7_before_send_mail in includes/classes.php is the best hook for it, I think.

  3. svoinea
    Member
    Posted 4 years ago #

    That was perfect. Thank you!

    I added this in the theme's functions.php and it works like a charm :

    add_action( 'wpcf7_before_send_mail', 'my_conversion' );

    function my_conversion( $cf7 )
    {
    $email = $cf7->posted_data["your-email"];
    $name = $cf7->posted_data["your-name"];
    insert_newsletter_subscriber($email, $name);
    }

    (Note: insert_newsletter_subscriber() is a custom function I wrote.)

  4. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    Good job!

  5. kevcpu
    Member
    Posted 4 years ago #

    Would this work if you have multiple forms or would you have to do something different?

  6. kevcpu
    Member
    Posted 4 years ago #

    Is there a way to differentiate between two forms in the functions.php file?

  7. converting2wp
    Member
    Posted 4 years ago #

    I haven't really tested this, but it looks as if there's a hidden value passed along with the other form data that is the number of the form. You could access it as

    $formid = $cf7->posted_data["_wpcf7"];

    and then base the action of function on the value of that $formid variable.

  8. converting2wp
    Member
    Posted 4 years ago #

    Okay, that didn't work.

    I needed to use

    if (isset($cf7->posted_data["some-field-unique-to-the-form"]) {

    ... do stuff...
    }

  9. gwbiffar
    Member
    Posted 4 years ago #

    I'm experiencing an issue when attempting to use the wpcf7_before_send_mail hook. I've copied svoinea's add_action and function into my theme's function.php file (Thank you for the post svoinea), but now when a user clicks submit on the form the gif by the send submit button continuously turns and the page never loads notification of the email's success/failure. Any thoughts on why this would occur? or any troubleshooting suggestions? Obviously, I'm not calling the insert newsletter function - at this point I'm just trying to echo the post data. The host's error logs show no events.

  10. gwbiffar
    Member
    Posted 4 years ago #

    It seems an echo or print statement in the function causes the behavior I observed... If I simply insert into my database, it works.

  11. converting2wp
    Member
    Posted 4 years ago #

    The echo may be in a context where it may not make sense (no page is actually being constructed) and/or there's a syntax error.

    In trying to make progress here (where, I too, didn't have any info from the server logs), I found this post that discussed how to use the WordPress logging:

    http://fuelyourcoding.com/simple-debugging-with-wordpress

    and I found the info logged there pointed me in the right direction. It's particularly helpful when something is keeping the page from being displayed -- as you describe.

  12. converting2wp
    Member
    Posted 4 years ago #

    @kevcpu: Going back to how to differentiate two forms

    $cf7->posted_data doesn't include the form ID, but it IS available as $cf7->id (duh!) and you may also find $cf7->title useful.

    -N [who thought her debugging days were really far behind her ... ;-)]

  13. titusbicknell
    Member
    Posted 4 years ago #

    I am trying to implement svoinea's db add method without success: I added:
    if ( function_exists('wpcf7_before_send_mail') )

    to see if the contact form hook is already present and the if fails. I also get a headers already sent error.

    Any help would be greatly appreciated.

    Titus

  14. converting2wp
    Member
    Posted 4 years ago #

    See the debugging link above for a way to write debug output to a file (rather than back to the browser, where, depending on the state of the rest of the http transaction it may not get all the way through).

    I'm still feeling like the blind person figuring out an elephant while I'm poking around the plugin structure, but in svoinea's example the line was

    add_action( 'wpcf7_before_send_mail', 'my_conversion' );

    and the second parameter was the function, not the first.

    If you want to be sure that there are no other functions tied to that hook, precede it with

    remove_all_filters ('wpcf7_before_send_mail');

  15. Marventus
    Member
    Posted 4 years ago #

    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!

  16. converting2wp
    Member
    Posted 4 years ago #

    Here's the code snippet:

    if ( 4 == $cf7->id ) {
    	... do stuff ...
    }
  17. Michael Simpson
    Member
    Posted 4 years ago #

    Thanks for the info in this thread. Based on that, I created a plugin to grab posted form data and put it in a database table automatically.

    Download Contact Form 7 to Database Plugin

    Please let me know how it works for you if you try it.

    Thanks.

  18. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    Thanks Michael. Well done!

  19. Marventus
    Member
    Posted 4 years ago #

    That sounds great! I'll give it a try in the next couple of days and let you know.

  20. DDT
    Member
    Posted 3 years ago #

    hi how can i echo the $cf7 data??

    It seems an echo or print statement in the function causes the behavior I observed... If I simply insert into my database, it works.

    i am trying this in functions.php

    function saveFormData($cf7) {
    	$email = $cf7->posted_data["email"];
    	$name = $cf7->posted_data["name"];
    	//insert_newsletter_subscriber($email, $name);
    	echo 'test'.$email.' : '.$name;
    }
  21. boupat
    Member
    Posted 3 years ago #

    Coul i have the code of insert_newsletter_subscriber() function .

    I'm newbie and need some sample of code to start !!

    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic