One useful functionality in WordPress is the ability to pre-populate form fields in WP Forms. This can be helpful when you want to provide a better user experience by automatically filling in certain fields for your users.
To achieve form pre-population in WP Forms, you can use the wpforms_process_filter
hook along with a custom function. Here’s an example code snippet that demonstrates how to pre-populate a field with a default value:
/**
* Pre-populate a WP Forms field with a default value.
*
* @param array $fields The form fields.
* @return array
*/
function wpsnippets_prepopulate_form_field( $fields ) {
// Set the default value for the field with ID 123.
$fields['field_123']['default'] = 'Default Value';
return $fields;
}
add_filter( 'wpforms_process_filter', 'wpsnippets_prepopulate_form_field' );
In the code snippet above, we define a custom function wpsnippets_prepopulate_form_field
that takes an array of form fields as a parameter. We then modify the default
value of the field with ID 123 to set it as ‘Default Value’. Finally, we return the modified array of fields.
By adding this code snippet to your theme’s functions.php
file or a custom plugin, the field with ID 123 in your WP Forms form will be pre-populated with the specified default value.
You can customize this code snippet to target different form fields by changing the field ID and the default value. Additionally, you can use dynamic values from WordPress, such as the current user’s name or email, to pre-populate the fields.
Examples
Example 1: Pre-populating a WP Forms form with user data
This use case demonstrates how to pre-populate a WP Forms form with user data. By using the wpforms_process_filter
hook, we can modify the form’s field values before it is displayed to the user.
function wpsnippets_prepopulate_wpforms( $fields, $form_data ) {
// Modify the 'name' field value
$fields['name'] = 'John Doe';
// Modify the 'email' field value
$fields['email'] = 'john.doe@example.com';
return $fields;
}
add_filter( 'wpforms_process_filter', 'wpsnippets_prepopulate_wpforms', 10, 2 );
In this code example, we define a custom function wpsnippets_prepopulate_wpforms
that takes two parameters: $fields
(an array of form field values) and $form_data
(an array containing form data). Inside the function, we modify the name
and email
field values to pre-populate the form with default values. Finally, we return the modified $fields
array.
Example 2: Pre-populating a WP Forms form with dynamic data
This use case demonstrates how to pre-populate a WP Forms form with dynamic data, such as the current user’s information. By using the wpforms_process_filter
hook and WordPress functions, we can retrieve and populate the form fields with the desired data.
function wpsnippets_prepopulate_wpforms( $fields, $form_data ) {
// Get the current user's display name
$current_user = wp_get_current_user();
$display_name = $current_user->display_name;
// Modify the 'name' field value
$fields['name'] = $display_name;
// Get the current user's email address
$user_email = $current_user->user_email;
// Modify the 'email' field value
$fields['email'] = $user_email;
return $fields;
}
add_filter( 'wpforms_process_filter', 'wpsnippets_prepopulate_wpforms', 10, 2 );
In this code example, we retrieve the current user’s display name and email address using the wp_get_current_user()
function. We then modify the name
and email
field values to pre-populate the form with the user’s information.
Example 3: Pre-populating a WP Forms form with query string parameters
This use case demonstrates how to pre-populate a WP Forms form with data from query string parameters. By using the wpforms_process_filter
hook and the $_GET
superglobal, we can retrieve the query string parameters and populate the form fields accordingly.
function wpsnippets_prepopulate_wpforms( $fields, $form_data ) {
// Check if the 'name' query string parameter is set
if ( isset( $_GET['name'] ) ) {
// Modify the 'name' field value
$fields['name'] = sanitize_text_field( $_GET['name'] );
}
// Check if the 'email' query string parameter is set
if ( isset( $_GET['email'] ) ) {
// Modify the 'email' field value
$fields['email'] = sanitize_email( $_GET['email'] );
}
return $fields;
}
add_filter( 'wpforms_process_filter', 'wpsnippets_prepopulate_wpforms', 10, 2 );
In this code example, we check if the name
and email
query string parameters are set using the isset()
function. If they are set, we sanitize the values using the sanitize_text_field()
and sanitize_email()
functions, respectively. Finally, we modify the name
and email
field values to pre-populate the form with the query string parameter values.