Search code examples
phpgravity-forms-plugingravityforms

Gravity Forms dynamic Confirmation not working with Session Variables


After form submission I check the value of the form field against an array of values. If there's a match, I set the session variables.

session_start();

add_action( 'gform_after_submission', 'validate_the_field', 10, 2 );
function validate_the_field( $entry, $form ) {

    $the_field= $entry["1"];
    if($the_field) {

        $all_fields = explode( ',', get_field('all_fields', 'options') );
        if ( in_array($the_field, $all_fields) ) {
            $_SESSION['authorized'] = 'authorized';
        }

    }
}

I am trying to display a dynamic confirmation messages based on the session variables but the result is always "You are not authorized".

add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {

    if ( $_SESSION['authorized'] == 'authorized' ) {
        $confirmation = "You are authorized.";
    } else {
        $confirmation = "You are not authorized.";
    }
    return $confirmation;
}

Any help is appreciated.


Solution

  • The gform_after_submission action is fired after the gform_confirmation filter. Try replacing it with the gform_entry_created action which fires before the confirmation filter.

    For more details on the order that Gravity Forms hooks are triggered, see the Gravity Forms Hook Reference:

    https://gravitywiz.com/gravity-forms-hook-reference/