重定向至小部件表单提交后的页面

时间:2015-08-15 作者:Amar

我有一个带有年龄、pincode等自定义字段的小部件表单。提交的表单数据将通过第三方API提交,该API将输出JSON。现在我想重定向到显示结果的页面。如何将结果从小部件重定向到页面。?

2 个回复
SO网友:Domain

如果您是通过Ajax执行此操作的,那么在收到响应后,可以通过JavaScript重定向

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

SO网友:Mayeenul Islam

在小部件内部,确保name 对于提交按钮:

<button type="submit" name="widget_submit">Submit</button>
在中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\' );
如果您想感谢某人提供了此代码块,请对@Sisir表示感谢his answer to me 从那里我学到了这一点。

结束

相关推荐

提交后重力表单-GFFormsModel::UPDATE_LEAD_FIELD_VALUE?

我在研究重力形态。此代码处理以下几个问题:add_action(\'gform_after_submission_14\', \'post_contract_update\', 10, 2); function post_contract_update($entry, $form) { $form =\'3\'; $lead = $entry[\'4\']; $statusvalue_update = \'Under Contract\';