I am trying to create a post when my Gravity Form submits and redirect to the post permalink on form confirmation.
See below the function which creates my post/order when my form submits using the gform_after_submission
action.
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, '/');
}
}
The above code works good and creates my order/post fine.
I am then trying to redirect to the newly created post when the form confirmations fires.
So i'm using gform_confirmation
filter but I can't find away to the pass the id to here.
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;
}
I cant redirect in the submission because my form is AJAX, and the redirect just happens in the ajax call, so I am having to try to use $confirmation
to modify the redirect.
Can anyone help me figure out a safe way to redirect to my newly created post via the submission?
Thanks
I am trying to create a post when my Gravity Form submits and redirect to the post permalink on form confirmation.
See below the function which creates my post/order when my form submits using the gform_after_submission
action.
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, '/');
}
}
The above code works good and creates my order/post fine.
I am then trying to redirect to the newly created post when the form confirmations fires.
So i'm using gform_confirmation
filter but I can't find away to the pass the id to here.
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;
}
I cant redirect in the submission because my form is AJAX, and the redirect just happens in the ajax call, so I am having to try to use $confirmation
to modify the redirect.
Can anyone help me figure out a safe way to redirect to my newly created post via the submission?
Thanks
2 Answers
Reset to default 1The entry object is available in both hooks, so how about saving the created post ID as meta on the entry with gform_update_meta()
:
gform_update_meta( $entry->id, 'order_id', $post_id );
Then retrieving it in the confirmation, using gform_get_meta()
:
$post_id = gform_get_meta( $entry->id, 'order_id' );
So basically I managed get Jacob Peattie idea working by using..
add_action('gform_pre_handle_confirmation', [ $this, 'create_order' ], 10, 2 );
instead of...
add_action('gform_after_submission_1', [ $this, 'create_order' ], 10, 2 );
But you can't specify the form with gform_pre_handle_confirmation
action so I had put this check in my create_order
function to make sure this only runs for this form.
// only allow this method to handle form 1
if (!array_key_exists('id', $form) || $form['id'] <> 1) {
return;
}