WordPress Elementor Form – Redirect post submission

I’ve been searching endlessly for a solution but i cant seem to get my custom code to not produce a parsererror message when trying to redirect.

I created a code snippet (that injects into functions.php) that creates a custom action for elementor form. This action basically creates a new post (for a custom post). In the ‘run’ function i can even error_log out the permalink of this new post.

Now i just want to redirect the user to this new post after the submission is successful, but it keeps throwing a parser error. The code does work, the post is created and the link is produced correctly via the $permalink variable i use.

Anyone can guide me the right way? Here is my code.

function add_new_createtravelplan_action($form_actions_registrar) {

    class CreateTravelPlan_Action_After_Submit extends ElementorProModulesFormsClassesAction_Base {

        /**
         * Get action name.
         *
         * Retrieve CreateTravelPlan action name.
         *
         * @since 1.0.0
         * @access public
         * @return string
         */
        public function get_name() {
            return 'createtravelplan';
        }

        /**
         * Get action label.
         *
         * Retrieve CreateTravelPlan action label.
         *
         * @since 1.0.0
         * @access public
         * @return string
         */
        public function get_label() {
            return esc_html__('Create Travel Plan', 'elementor-forms-create_travelplan-action');
        }

        /**
         * Run action.
         *
         * Create a new travel plan.
         *
         * @since 1.0.0
         * @access public
         * @param ElementorProModulesFormsClassesForm_Record  $record
         * @param ElementorProModulesFormsClassesAjax_Handler $ajax_handler
         */
        public function run($record, $ajax_handler) {
            
            
            $raw_fields = $record->get('fields');
            $fields = [];
            foreach ($raw_fields as $id => $field) {
                $fields[$id] = $field['value'];
                error_log($field['value']);
            }
            
            function bedrockCall($location, $startdate, $enddate, $person1name, $person1pref, $person2name, $person2pref, $person3name, $person3pref) {
                $response = generateTravel($location, $startdate, $enddate, $person1name, $person1pref, $person2name, $person2pref, $person3name, $person3pref);
                $data = json_decode($response, true);

                $itinerary = $data['generation'];

                // Find the position of the word "Day 1"
                $startPosition = strpos($itinerary, 'Day 1');

                // Extract the substring starting from "Day 1"
                $trimmedItinerary = substr($itinerary, $startPosition);

                return nl2br($trimmedItinerary);
            }
            
            
            $response = bedrockCall($fields['city'],$fields['start_date'],$fields['end_date'],$fields['person1Name'],$fields['person1Preferences'],$fields['person2Name'],$fields['person2Preferences'],$fields['person3Name'],$fields['person3Preferences']);  
            
            if ($response) {
                        // Prepare the post data
                    $post_data = array(
                        'post_type' => 'itinerary',
                        'post_title' => $fields['title'],
                        'post_status' => 'publish',
                        'meta_input' => array(
                            'trip_city' => $fields['city'],
                            'trip_start_date' => $fields['start_date'],
                            'trip_end_date' => $fields['end_date'],
                            'person1_name' => $fields['person1Name'],
                            'person1_preferences' => $fields['person1Preferences'],
                            'person2_name' => $fields['person2Name'],
                            'person2_preferences' => $fields['person2Preferences'],
                            'person3_name' => $fields['person3Name'],
                            'person3_preferences' => $fields['person3Preferences'],
                            'generated_itinerary' => $response,
                        ),
                    );
                
                    // Insert the custom post
                    $post_id = wp_insert_post($post_data);
                    $permalink = get_permalink($post_id);
                    //I CANT REDIRECT HERE - THE wp_redirect doesnt work, nor does adding <script> and redirecting
                    wp_redirect($permalink);
                    exit; // It's important to exit after redirecting
                                
            }
            
            
        } //close public function

        /**
         * Register action controls.
         *
         * CreateTravelPlan action has no input fields to the form widget.
         *
         * @since 1.0.0
         * @access public
         * @param ElementorWidget_Base $widget
         */
        public function register_settings_section($widget) {}

        /**
         * On export.
         *
         * CreateTravelPlan action has no fields to clear when exporting.
         *
         * @since 1.0.0
         * @access public
         * @param array $element
         */
        public function on_export($element) {}

    }

    $form_actions_registrar->register(new CreateTravelPlan_Action_After_Submit());

}
add_action('elementor_pro/forms/actions/register', 'add_new_createtravelplan_action');

I even tried to use JS – which threw a parsererror

Weird thing, no error on debug log, no error nginx log, none in php…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật