I have a multi step form built with Elementor in my Wordpress application. 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). Once the form has been successfully submitted I want to redirect to another page in the application.
I have been trying to use wp_redirect to do this but all I get is an error reported by the form (it only shows x parse error on the page). The code I am having trouble with is this part at the very end (everything else is working fine):-
//redirect after successful form submission
wp_redirect( home_url() );
exit();
I have tried different values for the wp_redirect argument even hard coding a URL but I still get the same issue.
Could anyone please give me a pointer to where I am going wrong??
The code is quite large but I thought it better to include it for completeness below:-
// 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 ;
You need to do it the Elementor way with record and handler.
Below you can find a general code to be used by anybody for an Elementor redirect:
//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 );
In the example above the form is redirected with an encoding in the GET parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.