Last updated on October 18, 2023

WP Forms dynamic field population

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Populate fields based on previous selections.

The code snippet below demonstrates how to dynamically populate fields in WP Forms using custom PHP functions and WordPress hooks.

// Define a custom function to populate the field value
function wpsnippets_populate_field_value($value, $field, $form_data) {
    // Check if the field ID matches the desired field
    if ($field['id'] === 'your_field_id') {
        // Set the desired field value dynamically
        $value = 'Your dynamic field value';
    }
    return $value;
}
add_filter('wpforms_field_value', 'wpsnippets_populate_field_value', 10, 3);

This code snippet utilizes the wpforms_field_value filter hook to modify the value of a specific field in WP Forms. The wpsnippets_populate_field_value function is defined to handle the field value population. Inside the function, you can check the field ID to determine which field you want to populate dynamically. In this example, we check if the field ID matches 'your_field_id' and set the desired value accordingly.

To use this code snippet, replace 'your_field_id' with the actual ID of the field you want to populate dynamically. You can also modify the logic inside the function to dynamically generate the field value based on your specific requirements.

This code snippet can be useful in scenarios where you need to pre-fill form fields with dynamic data. For example, you can populate a hidden field with the current user’s email address or populate a dropdown field with a list of categories from your WordPress site.

Examples

Example #1: Populate a dropdown field with a list of categories

This use case demonstrates how to populate a dropdown field in a WP Forms form with a list of categories from your WordPress site.

function wpsnippets_populate_dropdown_with_categories( $field, $form_id ) {
    if ( $field['type'] == 'select' && $field['inputType'] == 'dropdown' ) {
        $categories = get_categories();
        $choices = array();
        foreach ( $categories as $category ) {
            $choices[] = array(
                'label' => $category->name,
                'value' => $category->term_id,
            );
        }
        $field['choices'] = $choices;
    }
    return $field;
}
add_filter( 'wpforms_field_properties', 'wpsnippets_populate_dropdown_with_categories', 10, 2 );

This code example uses the wpforms_field_properties filter to modify the properties of a dropdown field in a WP Forms form. It checks if the field type is a dropdown and then retrieves a list of categories using the get_categories() function. It then loops through the categories and adds them as choices to the dropdown field.

Example #2: Populate a text field with the current user’s username

This use case demonstrates how to populate a text field in a WP Forms form with the username of the currently logged-in user.

function wpsnippets_populate_text_field_with_username( $field, $form_id ) {
    if ( $field['type'] == 'text' && $field['inputType'] == 'singleline' ) {
        $current_user = wp_get_current_user();
        $field['defaultValue'] = $current_user->user_login;
    }
    return $field;
}
add_filter( 'wpforms_field_properties', 'wpsnippets_populate_text_field_with_username', 10, 2 );

This code example uses the wpforms_field_properties filter to modify the properties of a text field in a WP Forms form. It checks if the field type is a text field and then retrieves the username of the current user using the wp_get_current_user() function. It then sets the default value of the text field to the username.

Example #3: Populate a hidden field with a custom value

This use case demonstrates how to populate a hidden field in a WP Forms form with a custom value.

function wpsnippets_populate_hidden_field_with_custom_value( $field, $form_id ) {
    if ( $field['type'] == 'hidden' ) {
        $field['defaultValue'] = 'Custom Value';
    }
    return $field;
}
add_filter( 'wpforms_field_properties', 'wpsnippets_populate_hidden_field_with_custom_value', 10, 2 );

This code example uses the wpforms_field_properties filter to modify the properties of a hidden field in a WP Forms form. It checks if the field type is a hidden field and then sets the default value of the field to a custom value.

Last updated on October 18, 2023. Originally posted on December 17, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *