Search code examples
phpwordpressformsgravity-forms-plugin

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.

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.


Solution

  • 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;
    }