Last updated on October 18, 2023

WP Forms dynamic default values

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

Set default values based on user input.

The code snippet below demonstrates how to set dynamic default values for fields in WP Forms. This can be useful when you want to pre-populate form fields with values based on certain conditions or user data.

/**
 * Set dynamic default value for a WP Forms field.
 *
 * @param array $field
 * @param array $form_data
 * @return array
 */
function wpsnippets_set_dynamic_default_value( $field, $form_data ) {
    // Set the default value based on your logic or data retrieval
    $default_value = 'Your dynamic default value';

    // Set the default value for the field
    $field['default'] = $default_value;

    return $field;
}
add_filter( 'wpforms_field_properties', 'wpsnippets_set_dynamic_default_value', 10, 2 );

In this example, we define a custom function wpsnippets_set_dynamic_default_value that takes two parameters: $field and $form_data. The $field parameter represents the field properties, and the $form_data parameter contains the submitted form data.

Inside the function, you can set the dynamic default value based on your requirements. In this case, we have set a static value for demonstration purposes, but you can replace it with your own logic or data retrieval.

Finally, we use the wpforms_field_properties filter to apply the function and modify the field properties. The modified field array is then returned.

By using this code snippet, you can dynamically set default values for WP Forms fields based on your specific needs.

Examples

Example 1: Setting a dynamic default value for a text field in WP Forms

This use case demonstrates how to set a dynamic default value for a text field in WP Forms. In this example, we will use a custom PHP function wpsnippets_get_current_user_name() to retrieve the current user’s name and set it as the default value for a text field.

function wpsnippets_get_current_user_name() {
    $current_user = wp_get_current_user();
    return $current_user->display_name;
}

add_filter( 'wpforms_field_default_value', 'wpsnippets_set_default_value', 10, 4 );
function wpsnippets_set_default_value( $default_value, $field_id, $form_data, $field ) {
    if ( $field_id === 123 ) { // Replace 123 with the actual field ID
        $default_value = wpsnippets_get_current_user_name();
    }
    return $default_value;
}

Explanation:

  • We define a custom PHP function wpsnippets_get_current_user_name() that retrieves the current user’s name using the wp_get_current_user() function.
  • We use the wpforms_field_default_value filter hook to modify the default value of a specific field (identified by its ID) in WP Forms.
  • Inside the filter callback function wpsnippets_set_default_value(), we check if the field ID matches the desired field and update the default value using our custom function wpsnippets_get_current_user_name().

Example 2: Setting a dynamic default value for a dropdown field in WP Forms

This use case demonstrates how to set a dynamic default value for a dropdown field in WP Forms. In this example, we will use a custom PHP function wpsnippets_get_categories() to retrieve a list of categories and set it as the default value for a dropdown field.

function wpsnippets_get_categories() {
    $categories = get_categories();
    $category_options = array();

    foreach ( $categories as $category ) {
        $category_options[ $category->term_id ] = $category->name;
    }

    return $category_options;
}

add_filter( 'wpforms_field_default_value', 'wpsnippets_set_default_value', 10, 4 );
function wpsnippets_set_default_value( $default_value, $field_id, $form_data, $field ) {
    if ( $field_id === 456 ) { // Replace 456 with the actual field ID
        $default_value = wpsnippets_get_categories();
    }
    return $default_value;
}

Explanation:

  • We define a custom PHP function wpsnippets_get_categories() that retrieves a list of categories using the get_categories() function.
  • Inside the wpsnippets_get_categories() function, we iterate through the categories and create an associative array where the category ID is the key and the category name is the value.
  • We use the wpforms_field_default_value filter hook to modify the default value of a specific field (identified by its ID) in WP Forms.
  • Inside the filter callback function wpsnippets_set_default_value(), we check if the field ID matches the desired field and update the default value using our custom function wpsnippets_get_categories().

Example 3: Setting a dynamic default value for a checkbox field in WP Forms

This use case demonstrates how to set a dynamic default value for a checkbox field in WP Forms. In this example, we will use a custom PHP function wpsnippets_get_user_roles() to retrieve the roles of the current user and set them as the default value for a checkbox field.

function wpsnippets_get_user_roles() {
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;
    return $user_roles;
}

add_filter( 'wpforms_field_default_value', 'wpsnippets_set_default_value', 10, 4 );
function wpsnippets_set_default_value( $default_value, $field_id, $form_data, $field ) {
    if ( $field_id === 789 ) { // Replace 789 with the actual field ID
        $default_value = wpsnippets_get_user_roles();
    }
    return $default_value;
}

Explanation:

  • We define a custom PHP function wpsnippets_get_user_roles() that retrieves the roles of the current user using the wp_get_current_user() function.
  • Inside the wpsnippets_get_user_roles() function, we access the roles property of the current user object to get an array of user roles.
  • We use the wpforms_field_default_value filter hook to modify the default value of a specific field (identified by its ID) in WP Forms.
  • Inside the filter callback function wpsnippets_set_default_value(), we check if the field ID matches the desired field and update the default value using our custom function wpsnippets_get_user_roles().
Last updated on October 18, 2023. Originally posted on November 26, 2023.

Leave a Reply