php - 使用 Elementor Forms API (Wordpress) 处理表单后重定向到主页
问题描述
我在我的 Wordpress 应用程序中使用 Elementor 构建了一个多步骤表单。我在 PHP 中使用 Elementor Forms API 来获取表单提交的数据并在 Wordpress (5.7.1) 中创建自定义帖子。成功提交表单后,我想重定向到应用程序中的另一个页面。
我一直在尝试使用wp_redirect来执行此操作,但我得到的只是表单报告的错误(它只在页面上显示x 解析错误)。我遇到问题的代码是最后的这一部分(其他一切都很好):-
//redirect after successful form submission
wp_redirect( home_url() );
exit();
我已经为 wp_redirect 参数尝试了不同的值,甚至对 URL 进行了硬编码,但我仍然遇到同样的问题。
谁能给我一个指向我哪里出错的指针?
代码非常大,但我认为最好将其包含在下面以保持完整性:-
// Create a custom post and populate custom fields from a standard Elementor form
add_action( 'elementor_pro/forms/new_record', function( $record, $handler ) {
//make sure its our form
$form_name = $record->get_form_settings( 'form_name' );
if ( 'Member_Details_Form' !== $form_name ) {
return;
}
// iterate all the fields and get them into an array
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
// assign each field to its own variable - member details
$address = $fields['member_address'];
$postcode = $fields ['member_post_code'];
$contact_number = $fields ['contact_number'];
$date_of_birth = $fields ['date_of_birth'];
// assign each field to its own variable - emergency contact details
$full_name = $fields ['full_name'];
$relationship = $fields['relationship'];
$emergency_address = $fields['address'];
$post_code = $fields['post_code'];
$telephone_number = $fields['telephone_number'];
// assign each field to its own variable - courses
$courses = $fields['courses'];
// assign each field to its own variable - sign off box
$consent_for_marketing = $fields['consent_for_marketing'];
$date = $fields['date'];
//check if a current members_details post exists - only ever want just one
$user_id = get_current_user_id(); //the logged in user's id
$post_type = 'member_details';
$posts = count_user_posts( $user_id, $post_type ); //count user's posts
//if no current members_details post exist create a new one
if( $posts < 1 ){
//user has no posts so we will create a new one
global $user_ID;
$new_post = array(
'post_title' => $name,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'member_details',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
//change the title of the post to the current logged in user
set_title($post_id);
}
//make sure we have the latest post_id
$latest_cpt = get_posts("post_type=member_details&numberposts=1");
$post_id = $latest_cpt[0]->ID;
//change the title of the post to the current logged in user
set_title($post_id);
//populate some custom fields on the custom post from the Elementor form data
// add member address details.
$field_key = "member_address";// map this to an elementor field
$value = $address;
update_field( $field_key, $value, $post_id );
// add member post code.
$field_key = "member_post_code";// map this to an elementor field
$value = $postcode;
update_field( $field_key, $value, $post_id );
// add member contact number
$field_key = "contact_number";// map this to an elementor field
$value = $contact_number;
update_field( $field_key, $value, $post_id );
// add member date_of_birth
$field_key = "date_of_birth";// map this to an elementor field
//$date_of_birth = new DateTime($date_of_birth);
$value = $date_of_birth;
update_field( $field_key, $value, $post_id );
// add emergency contact name
$field_key = "full_name";// map this to an elementor field
$value = $full_name;
update_field( $field_key, $value, $post_id ); // map this to an elementor field
// Save a checkbox or select value.
$field_key = "relationship";
$value = array($relationship);
update_field( $field_key, $value, $post_id );
// add emergency contact address details.
$field_key = "address";// map this to an elementor field
$value = $emergency_address;
update_field( $field_key, $value, $post_id );
// add emergency contact post code.
$field_key = "post_code";// map this to an elementor field
$value = $post_code;
update_field( $field_key, $value, $post_id );
// add emergency contact contact number
$field_key = "telephone_number";// map this to an elementor field
$value = $telephone_number;
update_field( $field_key, $value, $post_id );
// add info on courses they are interested in
$field_key = "courses";
$value = array($courses);
update_field( $field_key, $value, $post_id );
// add consent for marketing
$field_key = "consent_for_marketing";
$value = array($consent_for_marketing);
update_field( $field_key, $value, $post_id );
// add signature date
$field_key = "date";// map this to an elementor field
$value = $date;
update_field( $field_key, $value, $post_id );
//since a post exists make sure the user role is set to member
$current_user = wp_get_current_user();
$wp_user_object = new WP_User($current_user->ID);
$wp_user_object->add_role( 'member' );
//redirect after successful form submission
wp_redirect( home_url() );
exit();
}, 10, 2 ;
解决方案
您需要使用记录和处理程序以 Elementor 方式进行操作。
您可以在下面找到任何人用于 Elementor 重定向的通用代码:
//redirect URL to be setup
$redirect_url = '/after-form?encoding='.base64_encode($fields['email']);
//add to record the redirect URL
$redirect_to = $record->replace_setting_shortcodes( $redirect_url );
// Set redirect action to handler
$handler->add_response_data( 'redirect_url', $redirect_to );
在上面的示例中,表单使用 GET 参数中的编码重定向。
推荐阅读
- python - 用户提示 N 时代码未终止
- reactjs - 在使用 React 开发的 Grafana 插件中实现共享工具提示
- graphics - 如何在不使用数据和函数的情况下绘制一些图形?(乌龟模拟)
- android - 加载 json 文件因混淆而失败
- html - 如何在 react js 中建模此代码行:-
- json - Linux 使用数组更新 JSON
- json - 展开 JSON 对象数组以使用 PostgreSQL 记录
- ios - 如何修改已安装在 iPhone 上的应用程序的 Bundle ID?
- angular - Angular Routing 演示:如果注销则自动远离保护区(重新启动 canActivate 守卫)
- excel - 如何从 VBA 代码中排除某些工作表?