# Fill Gravity Forms Fields With the Logged-In User's Details

> Lets Gravity Forms fields fill themselves with the visitor's name, email, username or role when they are logged in, using simple parameter names.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: gravity-forms, prefill, users
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/gf-prefill-user-details/

## Code

```php
$wpide_gf_user_params = array(
    'user_email'        => 'user_email',
    'user_first_name'   => 'first_name',
    'user_last_name'    => 'last_name',
    'user_display_name' => 'display_name',
    'user_login'        => 'user_login',
);

foreach ( $wpide_gf_user_params as $wpide_param => $wpide_prop ) {
    add_filter( 'gform_field_value_' . $wpide_param, function ( $value ) use ( $wpide_prop ) {
        if ( '' !== (string) $value || ! is_user_logged_in() ) {
            return $value;
        }

        return (string) wp_get_current_user()->get( $wpide_prop );
    } );
}

add_filter( 'gform_field_value_user_role', function ( $value ) {
    if ( '' !== (string) $value || ! is_user_logged_in() ) {
        return $value;
    }

    $roles = (array) wp_get_current_user()->roles;

    return $roles ? (string) reset( $roles ) : '';
} );
```

## Notes

In a field's Advanced tab tick "Allow field to be populated dynamically" and enter one of the parameter names: user_email, user_first_name, user_last_name, user_display_name, user_login or user_role.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Fill Gravity Forms Fields With the Logged-In User's Details" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
