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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.