1

I have a widget form with custom fields like age,pincode etc.The submitted form data will be submitted through a 3rd party API which will output me JSON. Now I want to redirect to a page showing the results.How can i redirect to a page from the widget with the result.?

2 Answers 2

1

Inside the widget ensure a unique name for the submit button:

<button type="submit" name="widget_submit">Submit</button>

In functions.php:

function wpse20150815_processing_widget() {
   if( !isset( $_POST['widget_submit'] ) )
       return;

   //set an array for storing errors
   $errors = array();

   //process form data, and store errors in the $errors array
   if( empty( $_POST['pincode'] ) ) {
      $errors[] = __( 'Pincode cannot be empty', 'textdomain' );
   }

   //if there's an error in our data validation, don't proceed further
   if( !empty($errors) )
      return;

   //do what you want with the processed data
   //don't forget to sanitize form fields before storing data into database

   //i.e. inserting post content using the form data
   $post_id = wp_insert_post(...); //dummy presentation, use proper sanitization etc.

   if( !is_wp_error( $post_id ) ) {
      //if the post is inserted successfully, then redirect
      wp_redirect('path/you/desire/');
      exit;
   }
}
add_action( 'template_redirect', 'wpse20150815_processing_widget' );

If you want to thank someone for this code block, say thanks to @Sisir for his answer to me from where I learnt this.

0

If you are doing this via Ajax then after getting the response, you can redirect via JavaScript

window.location.href = 'url to be redirected to';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.