Last active
September 27, 2022 17:02
-
-
Save mheywood90/2400a0b0cd57193e6489 to your computer and use it in GitHub Desktop.
Contact Form 7 to External DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Contact Form 7 to External DB | |
Plugin URI: http://thedrawingroomcreative.com | |
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: The Drawing Room | |
Version: 0.1 | |
Author URI: http://thedrawingroomcreative.com | |
*/ | |
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' ){ | |
//get posted form fields | |
$field1 = $cf7->posted_data["your-field"]; | |
$field2 = $cf7->posted_data["your-field"]; | |
$field3 = $cf7->posted_data["your-field"]; | |
//insert into external db | |
$mydb->insert( | |
//name of external db table | |
'table_name', | |
//name of table columns and which fields to insert | |
array( | |
'column1' => $field1, | |
'column2' => $field2, | |
'column3' => $field3, | |
), | |
//field formats: %s = string, %d = integer, %f = float | |
array( | |
'%s','%s','%s' | |
) | |
); | |
} | |
} | |
} | |
add_action("wpcf7_before_send_mail", "wpcf7_send_to_external"); |
Automatically generate MySQL table and columns 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
Hello I setup this on my site but i have trouble making this work I have some questions, can i contact you via twitter or any social network? mine is @instxp