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…