我正在gform_after_submission操作中创建一个post,它在成功创建post时设置一个post ID变量。
https://docs.gravityforms.com/gform_之后_提交/
add_action('gform_after_submission_1', [ $this, 'create_order' ], 10, 2 );
public function create_order( $entry, $form ) {
// get the current cart data array
$data = self::data();
// user id
$user_id = get_current_user_id();
// 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;
// my order custom field updates go here...
}
}因为我的表单是通过ajax提交的,所以不能调用上面的header php重定向,因为重定向只会在ajax请求中发生。
我需要以某种方式将我的$post_id传递给重力形式的gform_confirmation过滤器。但我真的很想知道该怎么做。
https://docs.gravityforms.com/gform_确认/确认
add_filter('gform_confirmation_1', [ $this, 'order_confirmation' ], 10, 4 );
public function order_confirmation( $confirmation, $form, $entry, $ajax ) {
// update redirect to order
$confirmation = array( 'redirect' => get_permalink($post_id) );
// return confirmation
return $confirmation;
}如果有人有什么想法,那就太好了,谢谢。
发布于 2020-04-20 13:57:19
针对这种情况,一个麻烦的方法就是获得最新的create帖子。不是理想的,但只要没有人在彼此毫秒内创造秩序就行了。
public function order_confirmation( $confirmation, $form, $entry, $ajax ) {
// get latest created order
$order = get_posts([
'post_type' => 'purchase-order',
'numberposts' => 1
]);
// update redirect to order
$confirmation = array( 'redirect' => get_permalink($order[0]->ID) );
// return confirmation
return $confirmation;
}发布于 2022-05-23 13:44:11
考虑到gform_confirmation还允许提交后访问$confirmation、$form和$entry参数,您可以使用它来代替gform_after_submission。
https://wordpress.stackexchange.com/questions/364595
复制相似问题