All of our themes use Metabox plugin to add custom fields. Sometimes you may need to add additional fields to an existing metabox. This can be done with a simple function added to your child theme. Below is an example showing additional fields to obituary metabox of our Obsequy theme. All of our themes comes bundled with a basic child theme.
To add additional fields to the obituary_meta_box metabox created with the Metabox plugin via a child theme, follow these steps:
Steps to Add Fields via Child Theme:
-
Hook into
rwmb_meta_boxes
The Metabox plugin uses therwmb_meta_boxesfilter to register custom metaboxes. You can modify it in your child theme by adding new fields. -
Locate the Existing Metabox Key
Ensure theobituary_meta_boxis the correct metabox ID registered in the parent theme/plugin. -
Add Additional Fields in Your Child Theme’s
functions.php
Use the following code in your child theme’sfunctions.php:
function child_theme_add_obituary_meta_fields( $meta_boxes ) {
foreach ( $meta_boxes as &$meta_box ) {
if ( isset( $meta_box['id'] ) && $meta_box['id'] === 'obituary_meta_box' ) {
$meta_box['fields'][] = [
'name' => 'Cause of Death',
'id' => 'cause_of_death',
'type' => 'text',
'desc' => 'Enter the cause of death',
];
$meta_box['fields'][] = [
'name' => 'Burial Location',
'id' => 'burial_location',
'type' => 'text',
'desc' => 'Enter the burial location',
];
}
}
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'child_theme_add_obituary_meta_fields', 20 );
Modify the type accordingly based on your needs. See all the available field types with documentation here