Purpose

Automatically prefill a form with data before it is loaded. This functionality allows you to dynamically populate form fields with relevant data (e.g., user information) based on custom logic.

Usage

add_filter( "wr_form_builder_set_prefill_data", array( $this, "set_form_data" ), 10, 2 );

Parameters

  • $prefill_data (array): The data to prefill the form with. Initially empty and can be populated with key-value pairs where the keys correspond to form field identifiers.
  • $form (Form): The form object being populated.

Use Case

This functionality is useful when:

  • You want to prefill a form with existing data, such as user or customer information.
  • Prefilled data should override defaults but not entry-specific data.

Implementation Example

The following example populates a form identified by form_identifier with WooCommerce customer data:

add_filter( "wr_form_builder_set_prefill_data", array( $this, "set_form_data" ), 10, 2 );
 
/**
 * Prefill form with customer data.
 *
 * @param array $prefill_data
 * @param Form $form
 * @return array
 */
public function set_form_data( array $prefill_data, Form $form ): array {
    if ( $form->get_identifier() === "form_identifier" ) {
        $current_user = wp_get_current_user();
        $customer     = new \WC_Customer( $current_user->ID );
        
        $prefill_data = array(
            "first_name" => $customer->get_first_name(),
            "last_name"  => $customer->get_last_name(),
            "email"      => $customer->get_email(),
        );
    }
 
    return $prefill_data;
}