I'm creating a form plugin to handle forms that can be hooked into using actions/filters by developers.
My plug-in needs to be able to handle different forms with different sets of filters and I see 2 ways of doing this.
Method 1
Fire of specific hooks for each form.
So code like this could be called form within my plugin:
$formId = 'contact';
$errors = apply_filters('forms_validate_' . $formId, $errors, $data);
And could be used like so:
add_filter('forms_validate_contact', function($errors, $data){
if(empty($data['name'])){
$errors['name'] = 'Name is required';
}
return $errors;
} 10, 2)
Method 2
Pass a parameter to the calling function.
So code like this could be called form within my plugin:
$formId = 'contact';
$errors = apply_filters('forms_validate', $formId, $errors, $data);
And could be used like so:
add_filter('forms_validate', function($formId, $error, $data){
switch($formId){
case 'contact':
if(empty($data['name'])){
$errors['name'] = 'Name is required';
}
break;
}
return $errors;
}, 10, 3)
Are there any examples in the WordPress core where this sort of issue is tackled?
Is there a preferred method of dealing with this?