Using presence of gf_token to show or hide fields

Hello.

I am trying to hide fields in a form unless the gf_token is present.

I have this start:

/**
 * Check to see if the second user is viewing the form (based on whether the Gravity Forms token is present).
 *
 * @return boolean
 */
function prefix_does_token_exist() {
	return (bool) filter_input( INPUT_GET, 'gf_token', FILTER_SANITIZE_STRING );
}

I know how to get the field id, etc., but would this be appropriate for the render page just like shown in the following example?

https://community.gravityforms.com/t/how-to-change-hidden-field-to-visible-with-php-code/16239/2

I came up with this:

add_filter( 'gform_pre_render', 'conditionally_show_field' );
function conditionally_show_field( $form ) {

    // Replace 10 with your form ID
    if ( $form['id'] != 1 ) {
        return $form;
    }

   // Replace 2 with your field ID
    $field_id = 2;

    // Check if the current token exists
    if ( prefix_does_token_exist() ) {
        foreach ( $form['fields'] as &$field ) {
            if ( $field->id == $field_id ) {
                $field->visibility = 'visible';
            }
        }
    }
    return $form;
}

I would assume this would work. Does this look right? I need to make the form ID selection an array or set of fields, ie more than one. Any input is welcome.

-d

// Replace 2 with your field ID
$field_id

$field_ids = [2, 5, 8]; // Replace with your desired field IDs

if (prefix_does_token_exist()) {
    foreach ($form['fields'] as &$field) {
        if (in_array($field->id, $field_ids)) {
            $field->visibility = 'visible';
        }
    }
}

Thanks chat-gpt

/**
 *  show hidden fields in an array in a particular form
 *
 * @return boolean
 */

add_filter( 'gform_pre_render', 'conditionally_show_field' );
function conditionally_show_field( $form ) {

    // Replace 10 with your form ID
    if ( $form['id'] != 1 ) {
        return $form;
    }

   // Replace XX with your field ID
    $field_ids = [2, 5, 8]; 



    // Check if the current token exists
    if ( prefix_does_token_exist() ) {if (prefix_does_token_exist()) {
    foreach ($form['fields'] as &$field) {
        if (in_array($field->id, $field_ids)) {
            $field->visibility = 'visible';
            }
        }
    }
    return $form;
}

finished code for others.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.