我从来不用wordpress,所以我完全迷路了。我有一个wordpress网站,其中包含使用“联系人-表单-7”插件创建的表单...当用户在网站上提交表单时,我需要进行ajax调用以将表单数据发送到外部URLe ...我该怎么办?
我的ajax调用:
$.ajax({
method: 'post',
url: "external url",
data: JSON.stringify({
"env": "",
"application": "",
"operation": "",
"token": "",
"utente": "",
"param": "",
"data":{ DATA FROM FORM }
}),
dataType: 'json',
},
error: function(message) {
}发布于 2020-10-14 17:44:26
您可以使用“wpcf7_before_send_mail”过滤器将联系人form7提交的数据发送到使用CURL或wp_remote_post的任何网站。
add_action( 'wpcf7_before_send_mail*', 'wpcf7_submite_data_to_3rdparty' );
function wpcf7_submite_data_to_3rdparty( $contact_form ) {
global $wpdb;
if ( ! isset( $contact_form->posted_data ) && class_exists( 'WPCF7_Submission' ) ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$form_data = $submission->get_posted_data();
}
} else {
return '';
}
$body = array(
'name' => $form_data['name'],
'email' => $form_data['email'],
'comments' => $form_data['comments'],
);
$url = 'https://example.com';
$params = array(
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded'
),
'body' => $body
);
$response = wp_remote_post( $url, $params );
}为了处理Jquery事件,您可以使用事件侦听器。
document.addEventListener( 'wpcf7mailsent', function( event ) {
console.log('mail sent OK');
// Stuff
}, false ); 当Ajax表单提交成功完成且邮件已发送时,将触发wpcf7mailsent。当Ajax表单提交成功完成时,无论其他事件如何,wpcf7submit都会触发。
https://stackoverflow.com/questions/64335309
复制相似问题