Pre-Filling Ninja Forms Fields with Logged-In User Data

Problem:

How do I automatically pre-fill fields in a Ninja Forms form with logged-in user data like first name, last name, and email?

Or

How can I pre-fill Ninja Forms fields based on logged-in user information in WordPress?

Solution:

You can easily auto-fill fields in Ninja Forms based on the logged-in user’s meta data using built-in merge tags or a custom filter in WordPress.

Method 1: Using Merge Tags
  1. Go to Ninja Forms > Forms and edit your form.
  2. Select the field you want to auto-fill (e.g., First Name, Email).
  3. In the Advanced Settings section, find the “Default Value” option.
  4. Use merge tags like:
    • {user_meta:first_name} for first name
    • {user_meta:last_name} for last name
    • {user:email} for email
    • {user_meta:your_custom_meta_key} for custom user meta fields
Method 2: Using a Custom Filter (For Advanced Logic)

If you need more flexibility, you can use the ninja_forms_render_default_value filter:

add_filter('ninja_forms_render_default_value', 'populate_custom_fields', 10, 3);

function populate_custom_fields($default_value, $field_type, $field_settings) {
if (is_user_logged_in()) {
$user = wp_get_current_user();
if (isset($field_settings['key']) && $field_settings['key'] === 'first_name') {
return get_user_meta($user->ID, 'first_name', true);
}
if (isset($field_settings['key']) && $field_settings['key'] === 'last_name') {
return get_user_meta($user->ID, 'last_name', true);
}
}
return $default_value;
}

This approach works seamlessly for automatically pulling in user data into your forms.

Conclusion:

Using either merge tags or custom code, you can easily set up Ninja Forms to pre-fill fields with logged-in user information, making your forms more user-friendly and efficient.

Related Blog

Sign up for our newsletter to stay up to
date with tech news!