Skip to content

Instantly share code, notes, and snippets.

@jc4316son
Forked from mheywood90/cf7_external_db.php
Last active September 27, 2022 17:01
Show Gist options
  • Save jc4316son/99045bef27057e02e488057f359a5c3b to your computer and use it in GitHub Desktop.
Save jc4316son/99045bef27057e02e488057f359a5c3b to your computer and use it in GitHub Desktop.
Contact Form 7 to External DB 2018
<?php
/*
Plugin Name: Contact Form 7 to External DB
Plugin URI:
Description: This plugin uses the wpcf7_before_send_mail hook to post a specific form to an external database. Upon use the details for your external database will need to be entered. Private use only.
Author:
Version: 0.2
Author URI:
*/
function wpcf7_send_to_external ( $cf7 ) {
//external db details
$username = 'username';
$password = 'password';
$database = 'database';
$host = 'host_ip';
//create new wpdb instance
$mydb = new wpdb($username, $password, $database, $host);
//limit hook to only fire on particular form ID (optional)
if ( $cf7->id == 1 ) {
//get select box for different enquiry types (optional)
$type = $cf7->posted_data["your-select"];
//if form type is equal to above value (optional)
if ( $type == 'Form Name' ){
//code added for wordpress 4.9 2018
$cf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
//get posted form fields
//these are example form fields
$field1 = $data["name"];
$field2 = $data["email"];
$field3 = $data["address"];
$field4 = $data["city"];
$field5 = $data["state"];
$field6 = $data["zip"];
$field7 = $data["phone"];
//insert into external db
$mydb->insert(
//name of external db table
'table_name',
//name of table columns and which fields to insert
//these are example fields
array(
'name' => $field1,
'email' => $field2,
'address' => $field3,
'city' => $field4,
'state' => $field5,
'zip' => $field6,
'phone' => $field7
),
//field formats: %s = string, %d = integer, %f = float
array(
'%s','%s','%s','%s','%s','%s','%s'
)
);
}
}
}
add_action("wpcf7_before_send_mail", "wpcf7_send_to_external");
@jc4316son
Copy link
Author

Needed to add this code for the plugin to work in wordpress 4.9
//code added for wordpress 4.9 2018
$cf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();

@ladyfulton
Copy link

How do yo customize to make this work? I finally got it to validate and activate with no errors but it does not insert the data into the database. It just says sending forever. Could you please help?

`<?php
/*
Plugin Name: Contact Form 7 to External DB
*/

function wpcf7_send_to_external ( $cf7 ) {

//external db details
$username = 'username';
$password = 'password';
$database = 'database';
$host = 'localhost';

//create new wpdb instance
$seconddb = new wpdb(username, password, database, localhost);

	//limit hook to only fire on [contact-form-7 id="102" title="Operations Analyst-Pontoon Input"] (optional)
    if ( $cf7->id == 102 ) {

	//get select box for different enquiry types (optional)
	// $type = $cf7->posted_data["your-select"];

	//if form type is equal to above value (optional)
	// if ( $type == 'Form Name' ){

		//code added for wordpress 4.9 2018
		$cf7 = WPCF7_ContactForm::get_current();
			$submission = WPCF7_Submission::get_instance();
		$data = $submission->get_posted_data();
		
		
		//get posted form fields
		//these are example form fields
		$date = $data["Week Beginning"];
		$select = $data["Data Type"];
		$text = $data["Monday"];
		$text = $data["Tuesday"];
		$text = $data["Wednesday"];
		$text = $data["Thursday"];
		$text = $data["Friday"];
		$text = $data["Saturday"];
		$text = $data["Sunday"];
		$text = $data["Weekly Total"];
		$text = $data["Previous Week Totals"];
		
		
		//insert into external db
		$mydb->insert( 
			//name of external db table
			'seconddb_Pontoon_OpAnalyst_TotalActualHours',
			//name of table columns and which fields to insert 
			//these are example fields
			array( 
			    'Week Beginning' => $date-448, 
				'Data Type' => $menu-661,
				'Monday' => $text-66,
				'Tuesday' => $text-66,
				'Wednesday' => $text-66,
				'Thursday' => $text-66,
				'Friday' => $text-66,
				'Saturday' => $text-66,
				'Sunday' => $text-66,
				'Weekly Total' => $text-66,
				'Previous Week Totals' => $text-66

			),
			//field formats: %s = string, %d = integer, %f = float
			array( 
				'%s','%s','%s','%s','%s','%s','%s'
			) 
		);

	}

}

add_action("wpcf7_before_send_mail", "wpcf7_send_to_external");`

@RobinOlsen
Copy link

I put this code in the contact-form-functions.php first and that crashed the entire website - could no longer log into wordpress. Placed in functions.php (within the contact form 7 - includes plugin directory) and it crashed the system as well. Can anyone tell me where to place this code so it does not crash the system?

@arshidkv12
Copy link

Automatically generate table and columns and save to external DB https://wpdebuglog.com/forums/topic/contact-form-7-external-database-mysql-easy-method/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment