Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created March 1, 2017 16:59
Show Gist options
  • Save jaredatch/835334c73939bb35116377a50a3255a8 to your computer and use it in GitHub Desktop.
Save jaredatch/835334c73939bb35116377a50a3255a8 to your computer and use it in GitHub Desktop.
WPForms conditional form redirects
<?php
/**
* WPForms custom redirect
*
* @param string $url URL form will redirect to
* @param int $form_id Form ID
* @param array $fields Submitted form fields
* @return string
*/
function wpf_custom_redirect( $url, $form_id, $fields ) {
// Only consider changing the redirect if its for form #50
if ( '50' == $form_id ) {
// In the example below, we look at the submitted value for field #5
// If that value is "test", then we change the redirect URL
if ( !empty( $field['5']['value'] ) && 'test' == $field['5']['value'] ) {
$url = 'https://wpforms.com';
}
}
return $url;
}
add_filter( 'wpforms_process_redirect_url', 'wpf_custom_redirect', 10, 3 );
@joshuafredrickson
Copy link

For this to work, make sure the form's confirmation method is set to "redirect."

@slaFFik
Copy link

slaFFik commented Sep 1, 2017

Also, not $field but $fields should be in code.
@jaredatch, you might want to fix this typo, otherwise, the snippet above doesn't work.

@juanlcoto
Copy link

Hi @slaFFik,
Thank you very much for your contribution.
Is this snippet no longer working?

@slaFFik
Copy link

slaFFik commented Feb 4, 2021

@juanlcoto It should work if you use $fields in code instead of $field.

@EliMariaaaa
Copy link

EliMariaaaa commented Feb 12, 2021

Hi @slaFFik,

I followed the code snippet above and set the confirmation to redirect but it's not working. It's just redirecting to itself.

function wpf_custom_redirect( $url, $form_id, $fields ) {

// Only consider changing the redirect if its for form #50
$variation_id = 0;
$url = get_site_url();
if ( '797' == $form_id ) {
	
	// In the example below, we look at the submitted value for field #5
	// If that value is "test", then we change the redirect URL
	if ( !empty( $fields['4']['value'] ) && 2 == $fields['4']['value'] ) { //birthdays
		$variation_id = 806;
	} else if ( !empty( $fields['4']['value'] ) && 1 == $fields['4']['value'] ) { //encouragement
		$variation_id = 807;
	} else if ( !empty( $fields['4']['value'] ) && 4 == $fields['4']['value'] ) { //spiritual
		$variation_id = 809;
	} else if ( !empty( $fields['4']['value'] ) && 5 == $fields['4']['value'] ) { //greeting
		$variation_id = 810;
	} else if ( !empty( $fields['4']['value'] ) && 6 == $fields['4']['value'] ) { //holiday
		$variation_id = 808;
	}
	
	$url .= '/cart?add-to-cart=82&variation_id='.$variation_id;
}

return $url;
}
add_filter( 'wpforms_process_redirect_url', 'wpf_custom_redirect', 10, 3 );

I'm using the free version of WPForms. Am I missing something?

@EliMariaaaa
Copy link

EliMariaaaa commented Feb 13, 2021

I'm not sure why the code above doesn't work. I used wpforms_process_complete instead from here.

My complete code:

//Capture the wpform submit, and call the "processForm" function
add_action( 'wpforms_process_complete', 'processForm', 5, 4 );

function processForm( $form_fields, $entry, $form_data, $entry_id ) {

    $card_arr = array(797, 1104, 1103, 1102, 1101, 1038, 997, 996, 973, 812);
    global $wpdb;
    $form_id = $form_data['id'];
    $variation_id = 0;
    $url = get_site_url();

    // Get the full entry object
    $entry = wpforms()->entry->get( $entry_id );

    // Fields are in JSON, so we decode to an array
    $entry_fields = json_decode( $entry->fields, true );

    //$form_data contains the user inputs
    //here you could validate your form
    if(!in_array($form_id, $card_arr)) {
	exit();
    } else if ( in_array($form_id, $card_arr)) {
	if($form_id == 797){
		if ( $entry_fields[4]['value_raw'] == 2 ) { //birthdays
			$variation_id = 806;
		}
		if ( $entry_fields[4]['value_raw'] == 1 ) { //encouragement
			$variation_id = 807;
		}
		if ( $entry_fields[4]['value_raw'] == 4 ) { //spiritual
			$variation_id = 809;
		}
		if ( $entry_fields[4]['value_raw'] == 5 ) { //greeting
			$variation_id = 810;
		}
		if ( $entry_fields[4]['value_raw'] == 6 ) { //holiday
			$variation_id = 808;
		}
	}
	$url .= '/cart/?add-to-cart=82&variation_id='.$variation_id;
	wp_redirect($url);
	exit();
    }
}

@pixerize
Copy link

pixerize commented Dec 9, 2021

In order for this function to work, one must set desired WPForms confirmation type to "Redirect". I hope I could help someone. Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment