我正在使用联系表单7来创建我的表单,并且我有一个自定义数据库托管在应该包含相关数据的同一服务器中。
我想将联系人的数据从7存储到我的自定义数据库而不是wordpress数据库。
我现在在functions.php中执行以下操作,
add_action('wpcf7_before_send_mail', 'save_form');
function save_form($wpcf7) {
/* For connecting to database */
$dbuser = "user";
$dbpass = "pass";
$dbhost = "localhost";
$dbname = "cistom_db";
// Connect to server and select database.
$db = mysqli_connect($dbhost, $dbuser, $dbpass) or die("cannot connect");
mysqli_select_db($db, $dbname) or die("cannot select DB");
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$submited = array();
$submited['title'] = $wpcf7->title();
} else {
echo 'error';
}
$insert_query = "insert into candidate(title)values('" . $submited['title'] . "')";
$result = mysqli_query($db, $insert_query);
if (!$result) {
die('Invalid query: $insert_query : ' . mysqli_error($db));
}
}
然而,似乎没有什么工作在这里。有人可以帮忙吗?
答案 0 :(得分:3)
按照以下步骤在自定义表格中存储联系表单7数据:
1)创建自定义表
CREATE TABLE candidate(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50)
);
2)创建7个字段的联系表格
[text* title]
[submit "Send"]
3)将以下代码添加到function.php
function contactform7_before_send_mail( $form_to_DB ) {
//set your db details
$mydb = new wpdb('root','','cistom_db','localhost');
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB )
$formData = $form_to_DB->get_posted_data();
$title = $formData['title'];
$mydb->insert( 'candidate', array( 'title' =>$title ), array( '%s' ) );
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );
希望这适合你。