Support Forum
I am unsure the best way to approach this, and wondering if there is a way I can centralise my fields. I exported my fields from the builder and have in their own php file.
So I have a very long custom field group. I then need to use these fields in different ways. For example, the very long field group is form one, but form two has some extra fields at specific moments in it and some other fields do not show up, and form three uses a different configuration but still the same fields.
Is there a way that I can still manage the fields centrally, so that if I need to make a change to a field then I do not need to make it in three file locations? Or do I just need to manage the three versions.
Thanks!
Hello Yasmine,
You can define an array of custom fields that is not changed across field groups (reusable), then use the function array_merge()
to add more fields to the reusable field group to other field groups for your requirements. For example:
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
// Reusing field group 1
$field_group_1 = [
[
'name' => __( 'Datetime', 'your-text-domain' ),
'id' => 'datetime_jeq28wycry',
'type' => 'datetime',
]
];
$additional_fields = [
[
'name' => __( 'Custom Html', 'your-text-domain' ),
'type' => 'custom_html',
],
];
$field_group_2 = array_merge( $field_group_1, $additional_fields );
$meta_boxes[] = [
'title' => __( 'Field Group 1', 'your-text-domain' ),
'id' => 'field-group-1',
'post_types' => 'page',
'fields' => $field_group_1,
];
$meta_boxes[] = [
'title' => __( 'Field Group 2', 'your-text-domain' ),
'id' => 'field-group-2',
'post_types' => 'page',
'fields' => $field_group_2,
];
return $meta_boxes;
}