Adding action to Contact Form 7 submit w/ PHP hook

10,529

Solution 1

I came across your thread while looking for a similar solution to the same problem with CF7 - a hang on submission when trying to pass info to my own database on the back-end for a CRM.

I did not see an answer to this issue anywhere on the web in relation to CF7 integration, so thought I would post here what I found was the problem and how it was fixed. I'm no pro, but the solution works in testing, if anyone has anything to add to this solution please chime in

Basically, if you are using Wordpress and trying to pass the info into a CRM database, I am going to assume your database tables are not on the same database as your Wordpress site database. What you are then trying to do is establish two database connections simultaneously, but the reference ID is being reused for your Wordpress database when trying to connect to your CRM. I found this was the root cause of the hang on submission during testing.

I used a deprecated command from PHP 4 that still works in PHP 5, mysql_connect, :

mysql_connect('localhost', 'root', '', true);

By passing 'true' as your fourth parameter you are able to keep the connection separate from the one running for your Wordpress site. Did this and the CF7 submission doesn't hang, it submits to the CRM and sends it out as an e-mail simultaneously, no problem.

Note too though, if something is wrong with your syntax for the CRM data submission i.e. misnamed variable, etc.. it will also hang. If passing 'true' doesn't work check your code first to make sure it's clean.

If anyone reading this has an equivalent solution for this with 'mysqli' commands I'd be interested to know it, I tried it using mysqli and couldn't get it working.

Solution 2

If the submit is hanging after you attached your function, at least you know that it had an effect. I'm not terribly familiar with Contact Forms 7, but this is probably not the proper place for an echo, and my guess is that it is hanging because you are writing to the buffer and then it is trying to do a redirect (check your error logs). If you want to see the contents of $cf7, a better way to do it would be:

// first option, using print_r()
error_log(print_r($cf7, true));

// second option, using var_dump() if you need the additional output
ob_start();                    // start buffer capture
var_dump($cf7);                // dump the values
$contents = ob_get_contents(); // put the buffer into a variable
ob_end_clean();                // end capture
error_log($contents);          // log contents of $cf7

The contents of the $cf7 variable will then be in your PHP error log, which will tell you how to access the different components.

Share:
10,529

Related videos on Youtube

Ennui
Author by

Ennui

Updated on June 04, 2022

Comments

  • Ennui
    Ennui almost 2 years

    I'm having some trouble implementing a bit of custom functionality in the Contact Form 7 plugin for Wordpress.

    What I want to do is pretty straightforward. The contact form in question is a normal contact inquiry form, so I need to retain the usual functionality (mailing the data). However I also have a checkbox on the form that allows the sender to choose whether to subscribe to the client's mailing list in addition to mailing the contact inquiry.

    The client uses Bronto for their mass mailing (similar to CC or Mailchimp). Bronto has a "direct add" feature (more info here) that allows you to send parameters to add contacts to the Bronto account by embedding an image whose url contains the requisite parameters (email address, list to subscribe to, etc).

    I can construct the image url with the contact form parameters no problem, but actually getting the image request sent is a different matter. I am over my head both in PHP and JS here and not sure what course to take.

    Currently I'm using the wpcf7_before_send_mail php hook built into CF7 and this appears to allow me to collect the form data and build the URL. However, since the plugin uses AJAX and doesn't actually redirect to another page on form submission it seems I can't successfully use any kind of php output (echo, alert, even error_log), presumably because the server doesn't know what it's supposed to write to.

    In functions.php:

    add_action( 'wpcf7_before_send_mail', 'bronto_contact_add' );
    function bronto_contact_add( $cf7 ) {
        $emailcf = $cf7->posted_data['email'];
        echo $emailcf;
    }
    

    This is just a test to see if echo works - it does not. Instead the form just hangs on submission and I see the rotating loading gif forever.

    What I need to do is build the image url using parameters from the cf7 object (which I can do no problem) and then somehow send that image request to the Bronto server (this is the part I am confused about). If this was a normal form that redirected to another php page upon submission I would have no problem doing this at all, but it uses AJAX which I don't know much about so I'm pretty lost now.

    Can anybody help shed some light on how the best way to accomplish this might be?