繁体   English   中英

使用 Elementor 处理表单后重定向到主页 Forms API (Wordpress)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM