Purpose
This action allows you to use wonderful relation forms but do not use the database or wp_options funktionality, insteady cou can handle the form save programaticalls. therfore you have to set the option type: external in the from
Usage
add_action( 'wr_form_external_save', 'custom_form_save', 10, 1 );
Parameters
$form (Form)
: The form object, which provides details such as the form identifier.
Use Case
This functionality is useful when:
- The form should not store data in the default database table.
- Custom workflows need to be triggered upon form submission.
- Integration with external systems (e.g., WooCommerce, APIs) is required.
Implementation Example
The following example updates a WooCommerce customer profile whenever the form customer_update is submitted:
add_action( 'wr_form_external_save', 'custom_form_save', 10, 1 );
function custom_form_save( Form $form ) {
if ( $form->get_identifier() === 'FORM_IDENTIFIER' ) {
$data = array();
foreach ( $form->get_fields() as $field ) {
$data[ $field->get_identifier() ] = $field->get_value();
}
$current_user = wp_get_current_user();
$customer = new \WC_Customer( $current_user->ID );
$changes = false;
if ( ($data["first_name"] ?? null) !== $customer->get_first_name() ) {
$customer->set_first_name( $data["first_name"] );
$customer->set_billing_first_name( $data["first_name"] );
$changes = true;
}
if ( ($data["last_name"] ?? null) !== $customer->get_last_name() ) {
$customer->set_last_name( $data["last_name"] );
$customer->set_billing_last_name( $data["last_name"] );
$changes = true;
}
...
if ( $changes ) {
$customer->save();
}
}
}