Prefill WPForms With the Logged-In User's Details
Fills WPForms name and email fields with the logged-in visitor's details, so members and customers do not have to type them again.
Opens WPIDE on
Code
add_filter( 'wpforms_field_properties', function ( $properties, $field, $form_data ) {
if ( ! is_user_logged_in() || ! in_array( $field['type'], array( 'name', 'email' ), true ) ) {
return $properties;
}
$user = wp_get_current_user();
if ( 'email' === $field['type'] && empty( $properties['inputs']['primary']['attr']['value'] ) ) {
$properties['inputs']['primary']['attr']['value'] = $user->user_email;
}
if ( 'name' === $field['type'] && ( empty( $field['format'] ) || 'simple' === $field['format'] ) && empty( $properties['inputs']['primary']['attr']['value'] ) ) {
$properties['inputs']['primary']['attr']['value'] = $user->display_name;
}
return $properties;
}, 10, 3 );
Notes
Works with the Simple name format and email fields; fields with their own default value are left alone.
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.