# Prefill Contact Form 7 With the Logged-In User's Details

> Fills the name and email fields of Contact Form 7 forms with the details of the logged-in visitor, so members do not have to type them again.

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

## Code

```php
// Form field name => user detail (display_name, user_email, first_name, last_name).
$wpide_cf7_user_map = array(
    'your-name'  => 'display_name',
    'your-email' => 'user_email',
);

add_filter( 'wpcf7_form_tag', function ( $tag ) use ( $wpide_cf7_user_map ) {
    if ( ! is_user_logged_in() || empty( $tag->name ) || ! isset( $wpide_cf7_user_map[ $tag->name ] ) || ! empty( $tag->values ) ) {
        return $tag;
    }

    $user  = wp_get_current_user();
    $value = (string) $user->get( $wpide_cf7_user_map[ $tag->name ] );

    if ( '' !== $value ) {
        $tag->values     = array( $value );
        $tag->raw_values = array( $value );
    }

    return $tag;
} );
```

## Notes

Uses the default field names your-name and your-email; change the map at the top for your own field names.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Prefill Contact Form 7 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/
