Assistance Needed to Redirect Forms with "YOUR COMMENT" Labeled Submit Button to Specified URL

// Redirects to ‘Email to different departments – myQmd + PSBQ tm’ after Gravity Form is submitted
// when the submit button is labeled ‘YOUR COMMENT’.
add_action(‘gform_after_submission’, ‘redirect_after_gform_submission’, 10, 2);
function redirect_after_gform_submission($entry, $form) {
$submit_button_label = ‘YOUR COMMENT’;

// Check if the submit button label matches
if (isset($_POST[‘gform_submit’]) && $_POST[‘gform_submit’] === $submit_button_label) {
$redirect_url = ‘Email to different departments – myQmd + PSBQ tm’;
wp_redirect($redirect_url);
exit;
}
}

I would like to redirect to a specific URL after clicking the SUBMIT button in all forms, as long as the button is labeled as “YOUR COMMENT.” I attempted to add the above-mentioned code to the functions.php file, but it resulted in a critical error. I need assistance in overriding the Notification and Confirmation and achieving the following: redirecting all forms with a “YOUR COMMENT” labeled SUBMIT button to a designated URL. Can you please provide guidance on how to accomplish this?

It seems you’ve mixed something up. The $_POST[‘gform_submit’] bit is going to get you the ID of the form that’s been submitted, not what’s written on the submit button. So, if you want to redirect after your form is submitted, and you want this to happen based on what’s written on the submit button, you need a different approach.

So, here’s what I’ve got for you:

// Let's use a filter to change the confirmation based on what's on the submit button
add_filter( 'gform_confirmation', 'redirect_based_on_button', 10, 4 );

function redirect_based_on_button( $confirmation, $form, $entry, $ajax ) {
    // You want the button to say 'YOUR COMMENT', right?
    $submit_button_label = 'YOUR COMMENT';
    
    // Now, let's check if the label on the button matches
    if ( RGFormsModel::get_current_page() == GFFormDisplay::get_max_page_number( $form ) && rgpost( 'gform_submit' ) == $form['id'] && rgpost( 'gform_original_submit_button' ) == $submit_button_label ) {
        // Now, here's where you want to go after the form is submitted
        $redirect_url = 'http://wherever-you-want-to-go.com';
        // And here's where we make the magic happen
        $confirmation = array( 'redirect' => $redirect_url );
    }

    return $confirmation;
}

Just replace ‘http://wherever-you-want-to-go.com’ with the URL you want to redirect to. And if the button says something different, replace ‘YOUR COMMENT’ too.

1 Like