Difficult To Filter Title
I’m working with the acf_form()
feature and I’m also using 'post_title' => true,
.
I’ve got a filter going to rename the title:
function acf_prepare_title_field($field)
{
if (is_wc_endpoint_url('my-pets') || is_checkout()) {
$field['label'] = __("Pet Name", 'mypets');
}
return $field;
}
add_filter('acf/prepare_field/name=_post_title', array(&$this, 'acf_prepare_title_field'));
I think there is a design issue here.
The prepare_field
filter seems to be built with the expectation that the field will have a unique name.
However _post_title
is the same for every type, and the $field
doesn’t have any wider context about what it is filtering.
It has resulted in me having to write some brittle code locking the form down to only being used in a few places so that I can guess that it is the correct form I’m renaming the title of.
Otherwise it will rename all forms to use this custom title label.
Possible solutions are to provide more granular _post_title
filters like _post_title_{post_type}
, or to pass some more context into the filter like the post type or something to be able to identify what context it’s being filtered in.
Possible bug / missed override
When the validation fails the message still uses “The Title” rather than my updated label:
Label: Pet Name *
Validation message: Title value is required
I’m still trying to find a way to fix this.