[英]Wordpress : Redirect Page after sign up failed - Essential Addons for Elementor
[英]Redirect to home page after form processing with Elementor Forms API (Wordpress)
我在我的 Wordpress 應用程序中使用 Elementor 構建了一個多步驟表單。 I am using the Elementor Forms API in PHP to grab data submitted by the form and create a custom post within 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 參數中的編碼重定向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.