当我的重力形式提交并重定向到表单确认时的post permalink时,我试图创建一个帖子。
请参阅下面的函数,该函数在我的表单使用gform_after_submission操作提交时创建post/order。
add_action('gform_after_submission_1', [ $this, 'create_order' ], 10, 2 );
/**
* ajax reload the cart summary
* @param object $entry
* @param array $form
* @return void
*/
public function create_order($entry,$form)
{
// convert keys to admin labels
$entry = self::admin_labels($entry,$form);
// get the current cart data array
$data = self::data();
// create an order array
$order = [
'post_author' => User::$id,
'post_content' => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
'post_type' => 'purchase-order',
'post_status' => 'publish'
];
// create order post using an array and return the post id
$result = wp_insert_post($order);
// if post id and is not a wp error then
if($result && !is_wp_error($result)) {
// get the id
$post_id = $result;
// update gform meta with the order id
gform_update_meta($entry['id'],'order_id',$post_id );
// create our order reference number
$order_ref = str_pad($post_id,5,'0',STR_PAD_LEFT);
// create our title
$title = 'Order #' . $order_ref;
// new order array of updates
$order = [
'ID' => $post_id,
'post_title' => $title,
'post_name' => $order_ref
];
// update the order with new details
wp_update_post($order);
// set the the order type as purchase order
wp_set_post_terms($post_id,'purchase-order','order_type');
// update all the order custom fields
self::update_order_fields($post_id,User::$id,$data,$entry);
// unset cookie
unset($_COOKIE['wm_cart']);
// re set cookie and backdate to end cookie
setcookie('wm_cart', null, -1, '/');
}
}以上代码工作良好,并创建了我的订单/张贴罚款。
然后,当表单确认触发时,我试图重定向到新创建的帖子。
所以我使用的是gform_confirmation过滤器,但是我找不到把id传递到这里的方法。
add_filter('gform_confirmation_1', [ $this, 'order_confirmation' ], 10, 4 );
/**
* ajax reload the cart summary
* @param array $confirmation
* @param array $form
* @param object $entry
* @param array $ajax
* @return array $confirmation
*/
public function order_confirmation($confirmation,$form,$entry,$ajax)
{
// get order id from gform get meta
$post_id = gform_get_meta($entry['id'],'order_id');
// confirmation
$confirmation = array( 'redirect' => get_permalink( $post_id ) );
// return confirmation
return $confirmation;
}我不能在提交中重定向,因为我的表单是AJAX,重定向只发生在ajax调用中,所以我不得不尝试使用$confirmation来修改重定向。
有人能帮我找到一个安全的方法,通过提交的方式重定向到我新创建的帖子吗?
谢谢
发布于 2020-05-04 14:38:31
条目对象在两个钩子中都是可用的,那么在带有gform_update_meta()的条目上将创建的post ID保存为meta如何:
gform_update_meta( $entry->id, 'order_id', $post_id );然后在确认中检索它,使用gform_get_meta():
$post_id = gform_get_meta( $entry->id, 'order_id' );发布于 2020-05-04 17:14:29
因此,基本上,我设法使雅各布·皮蒂的想法通过使用..。
add_action('gform_pre_handle_confirmation', [ $this, 'create_order' ], 10, 2 );而不是..。
add_action('gform_after_submission_1', [ $this, 'create_order' ], 10, 2 );但是您不能使用gform_pre_handle_confirmation操作指定表单,所以我已经将此检查放在create_order函数中,以确保只对此表单运行。
// only allow this method to handle form 1
if (!array_key_exists('id', $form) || $form['id'] <> 1) {
return;
}https://wordpress.stackexchange.com/questions/365814
复制相似问题