# Using presence of gf\_token to show or hide fields

**URL:** <https://community.gravityforms.com/t/using-presence-of-gf-token-to-show-or-hide-fields/17880>\
**Category:** Get Help\
**Tags:** custom, gf\_token, conditional-logic, query-string\
**Created:** [July 21, 2024, 11:47am UTC](https://community.gravityforms.com/t/using-presence-of-gf-token-to-show-or-hide-fields/17880 "2024-07-21T11:47:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![user6699ca1d8b4d2153](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@user6699ca1d8b4d2153](https://community.gravityforms.com/u/user6699ca1d8b4d2153)\
**Post date:** [July 21, 2024, 11:47am UTC](https://community.gravityforms.com/t/using-presence-of-gf-token-to-show-or-hide-fields/17880/1 "2024-07-21T11:47:04Z")

</div>

Hello.

I am trying to hide fields in a form unless the gf\_token is present.

I have this start:

```auto
/**
 * 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](https://community.gravityforms.com/t/how-to-change-hidden-field-to-visible-with-php-code/16239/2)

I came up with this:

```auto
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

---

<div class="post-metadata">

**Author:** ![user6699ca1d8b4d2153](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@user6699ca1d8b4d2153](https://community.gravityforms.com/u/user6699ca1d8b4d2153)\
**Post date:** [July 21, 2024, 12:00pm UTC](https://community.gravityforms.com/t/using-presence-of-gf-token-to-show-or-hide-fields/17880/2 "2024-07-21T12:00:07Z")

</div>

```auto
// 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

---

<div class="post-metadata">

**Author:** ![user6699ca1d8b4d2153](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@user6699ca1d8b4d2153](https://community.gravityforms.com/u/user6699ca1d8b4d2153)\
**Post date:** [July 21, 2024, 12:04pm UTC](https://community.gravityforms.com/t/using-presence-of-gf-token-to-show-or-hide-fields/17880/3 "2024-07-21T12:04:30Z")

</div>

> [@user6699ca1d8b4d2153](#):
>
> ```auto
> if (prefix_does_token_exist()) {
> foreach ($form['fields'] as &$field) {
> if (in_array($field->id, $field_ids)) {
> $field->visibility = 'visible';
> 
> ```

```auto
/**
 * 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [August 20, 2024, 12:05pm UTC](https://community.gravityforms.com/t/using-presence-of-gf-token-to-show-or-hide-fields/17880/4 "2024-08-20T12:05:14Z")

</div>

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