[英]How can I save user input from a Contact Form 7 into a Custom Table in my Wordpress database?
I have a contact form called Check In
that I want to store in the table wpxo_checkin_forms
.我有一个名为Check In
的联系表单,我想将其存储在表wpxo_checkin_forms
中。 The columns are: userid
, firstname
, lastname
, weight
, checkindate
, energylevel
(3 radio button options), missmeal
(two radio button options), and missworkout
(two radio button options).这些列是: userid
、 firstname
、 lastname
、 weight
、 checkindate
、 energylevel
(3 个单选按钮选项)、 missmeal
(两个单选按钮选项)和missworkout
(两个单选按钮选项)。
I found this code here that I tried to modify and put in my child theme's function.php, but when I submit the form, it does not save in the wpxo_checkin_forms
table.我在这里找到了这段代码,我试图修改并放入我的子主题的 function.php,但是当我提交表单时,它没有保存在wpxo_checkin_forms
表中。
What am I doing wrong here?我在这里做错了什么?
add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
if ( ! class_exists( 'WPCF7_Submission' ) ) {
return;
}
$submission = WPCF7_Submission::get_instance();
if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
$posted_data = $submission->get_posted_data();
save_posted_data( $posted_data, $form->id );
}
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
if ( 1522 !== $form_id ) {
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'wpxo_checkin_forms',
array(
'userid' => $posted_data['userid'],
'firstname' => $posted_data['first-name'],
'lastname' => $posted_data['last-name'],
'checkindate' => $posted_data['checkin-date'],
'weight' => $posted_data['weight'],
'energylevel' => $posted_data['energy-level'],
'missmeal' => $posted_data['miss-meal'],
'missworkout' => $posted_data['miss-workout']
),
array( '%s' )
);
}
}```
Did you debug this code?你调试过这段代码吗? First make sure that your function runs, If it is running then check the email condition.首先确保您的 function 运行,如果它正在运行,则检查 email 条件。 you have to stop emailing for inserting testing.您必须停止发送电子邮件以插入测试。
The following code should work:以下代码应该可以工作:
add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
if ( ! class_exists( 'WPCF7_Submission' ) ) {
return;
}
$submission = WPCF7_Submission::get_instance();
if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
$posted_data = $submission->get_posted_data();
save_posted_data( $posted_data, $form->id );
}
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
if ( 1522 !== $form_id ) {
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'wpxo_checkin_forms',
array(
'userid' => $posted_data['userid'],
'firstname' => $posted_data['first-name'],
'lastname' => $posted_data['last-name'],
'checkindate' => $posted_data['checkin-date'],
'weight' => $posted_data['weight'],
'energylevel' => $posted_data['energy-level'],
'missmeal' => $posted_data['miss-meal'],
'missworkout' => $posted_data['miss-workout']
)
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.