Redirect gravity form on submission to post just created

I have a gravity form which creates a custom post and, currently, on submission it displays the confirmation message as per the form settings.

Rather than display the confirmation message, I want to redirect the page to the post that the form has just created.

Read More

Gravity Forms has a filter, gform_confirmation (or gform_confirmation_[form_id] to target a specific form) which works like this:

<?php
add_filter('gform_confirmation', 'reroute_confirmation', 10, 4);
function reroute_confirmation($form, $lead, $confirmation, $ajax) {
    $confirmation = array('redirect' => 'http://target_url.com');
    return $confirmation;
} ?>

My problem is I don’t know what the URL is because it will be determined by the slug of the custom post created by the form.

I tried a var_dump of $lead and $form to see if it told me the new post id, but they don’t seem to.

I tried the same with the gform_entry_post_save filter, but no joy either there.

I’m guessing some hook will reveal the post id (or slug) and I can work from there to use the gform_confirmation filter to redirect, I just can’t find the right one.

Related posts

Leave a Reply

2 comments

  1. The hook “gform_after_submission” has the $entry object with details of the newly created post available to it, including the post_id.

    So armed with the post_id I can then redirect.

    <?php
    add_action('gform_after_submission', 'redirect_on_post', 10, 2);
    function redirect_on_post($entry, $form) {
        $post_id = $entry['post_id'];
        $post_name = get_permalink('$post_id');
        wp_redirect($post_name);
        exit;
    }
    
  2. Your solution actually didn’t work for me (the form just seemed to sit there after I submitted it – the page didn’t change at all and the little “please wait” animation kept going forever). I think it might be something to do with AJAX, which I don’t understand. 🙂

    Inside the gform_confirmation action you actually also have access to the post ID, except in the $lead array instead of $entry. So you can do:

    function my_gform_confirmation($confirmation, $form, $lead, $ajax){
    $pid = $lead['post_id'];
    $confirmation = array("redirect" => get_site_url() . "/?p=$pid");
    return $confirmation;
    }
    

    This will send the user back to the original post. Even if it has a long permalink address, the above will redirect you to the right post. There’s no need to resolve it to the full link.