Reputation: 59
My website in wordpress. I want to send automatically contact form 7 value to different database table. For example one is wordpress database table & another one is php database table. how i can fixed this issue?
Upvotes: 4
Views: 1499
Reputation: 174
Follow the Below Steps and add code in functions.php:
1) Create custom table in your custom database
CREATE TABLE candidate(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50)
);
2) Create contact form 7 fields
[text* title]
[submit "Send"]
3) Add Below code to function.php
function contactform7_before_send_mail( $form_to_DB ) {
//set your db details
global $wpdb;
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB )
$formData = $form_to_DB->get_posted_data();
$title = $formData['title'];
/************* Insert in wordpress database ***********/
$wpdb->insert( 'candidate', array( 'title' =>$title ), array( '%s' ) );
/************ For Php Mysql Database (Create connection first) ********/
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (title)
VALUES ($title)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );
Upvotes: 2
Reputation: 695
Add below code to your function.php file.
function custom_db_wpcf7_before_send_mail($form7_data) {
//set your database details
$custom_db = new wpdb('user','password','custom_db','host');
$form7_data = WPCF7_Submission::get_instance();
if ($form7_data)
$post_data = $form7_data->get_posted_data();
$title = $post_data['title'];
$custom_db->insert('custom_table', array('field' => $value), array( '%s' ));
}
remove_all_filters('wpcf7_before_send_mail');
add_action('wpcf7_before_send_mail', 'custom_db_wpcf7_before_send_mail' );
}
Upvotes: 0