Dispatch Rule
apply_filters_ref_array( 'vikappointments_dispatch_custom_field_rule', bool $did, mixed $field, mixed $value, array &$data )
Fires before dispatching a custom field rule.
Description
Trigger hook to allow external plugins to dispatch a custom rule during the saving process.
This process is mainly used to collect the details specified by the customers into different database tables.
In case the rule needs to validate the entered data, it is possible to throw an exception to abort the saving process.
It is possible to access the rule of the field with $field->get('rule')
.
Parameters
- $did
-
(bool) True to avoid dispatching the default system rules.
- $field
-
(VAPCustomField) The custom field instance.
- $value
-
(mixed) The value of the field set in request.
- &$data
-
(array) The array data to fill-in in case of specific rules (name, e-mail, etc...).
Example
The example registers the specified country code into the customer database table.
/**
* Trigger hook to allow external plugins to dispatch a custom rule.
*
* @param boolean $did True to avoid dispatching the default system rules.
* @param mixed $field The custom field instance.
* @param mixed $value The value of the field set in request.
* @param array &$data The array data to fill-in in case of
* specific rules (name, e-mail, etc...).
*/
add_filter('vikappointments_dispatch_custom_field_rule', function($did, $field, $value, &$data)
{
// make sure we are dispatching a field with "country" rule
if ($field->get('rule') !== 'country' || $did)
{
// different rule or already completed
return (bool) $did;
}
// register country code within the array data to be saved into the customer record
$data['country_code'] = $value;
return true;
}, 10, 4);
Example #2
The example below explains how to extend the form validation via PHP for a field assigned to a specific rule.
add_filter('vikappointments_dispatch_custom_field_rule', function($did, $field, $value, &$data)
{
// make sure we are dispatching a field with "ticket" rule
if ($field->get('rule') !== 'ticket' || $did)
{
// different rule or already completed
return (bool) $did;
}
// validate the provided ticket via API to our own ticketing platform
$response = wp_remote_get('https://your-ticket-platform.com/api/v1/ticket/' . $value);
if (wp_remote_retrieve_response_code($response) != 200)
{
throw new Exception('The provided ticket does not exist.', 404);
}
return true;
}, 10, 4);
Changelog
Version | Description |
---|---|
1.2 | Introduced. |