Developing Custom Fields via Meta Box for WordPress
Meta Box is an alternative to ACF for creating custom fields in WordPress. Main advantage over ACF: the free version is complete for most tasks, all extensions (Relationships, Group, Frontend Submission) are purchased separately and cheaper, and the API is built via PHP without GUI dependency—convenient for code-first approach. Configuring a field group takes from a few hours to 1 day.
Field Registration via PHP API
add_filter('rwmb_meta_boxes', function (array $meta_boxes): array {
$meta_boxes[] = [
'id' => 'project_details',
'title' => 'Project Details',
'post_types' => ['project'],
'priority' => 'high',
'fields' => [
[
'id' => 'project_client',
'name' => 'Client',
'type' => 'text',
'required' => true,
'placeholder' => 'Client company name',
'size' => 50,
'columns' => 6, // 6/12 = half width
],
[
'id' => 'project_year',
'name' => 'Year',
'type' => 'number',
'min' => 2000,
'max' => 2030,
'std' => date('Y'),
'columns' => 3,
],
[
'id' => 'project_status',
'name' => 'Status',
'type' => 'select',
'options' => [
'active' => 'Active',
'completed' => 'Completed',
'paused' => 'Paused',
],
'std' => 'completed',
'columns' => 3,
],
[
'id' => 'project_url',
'name' => 'Project URL',
'type' => 'url',
'placeholder' => 'https://',
],
[
'id' => 'project_budget',
'name' => 'Budget (k)',
'type' => 'number',
'min' => 0,
'step' => 10,
'placeholder' => '0',
'admin_only' => true, // visible in /wp-admin only
],
[
'id' => 'project_description',
'name' => 'Internal Description',
'type' => 'textarea',
'rows' => 4,
'desc' => 'Not displayed on site, for internal use only',
],
],
];
return $meta_boxes;
});
Image and File Fields
[
'id' => 'project_cover',
'name' => 'Project Cover',
'type' => 'image_advanced',
'max_file_uploads' => 1,
'force_delete' => false,
'image_size' => 'large',
'columns' => 6,
],
[
'id' => 'project_gallery',
'name' => 'Gallery',
'type' => 'image_advanced',
'max_file_uploads' => 15,
'image_size' => 'medium',
'add_to' => 'end',
],
[
'id' => 'project_brief',
'name' => 'Brief (PDF)',
'type' => 'file_advanced',
'mime_type' => 'application/pdf',
'max_file_uploads' => 1,
],
Frontend retrieval:
// Single image
$cover = rwmb_meta('project_cover', ['size' => 'large'], $post_id);
if ($cover) {
echo '<img src="' . esc_url($cover[0]['url']) . '" alt="' . esc_attr($cover[0]['alt']) . '">';
}
// Gallery
$gallery = rwmb_meta('project_gallery', ['size' => 'medium'], $post_id);
foreach ($gallery as $image) {
echo '<img src="' . esc_url($image['url']) . '" loading="lazy">';
}
Group — Nested Fields
Meta Box Group (paid addon) allows creating nested groups and cloneable blocks:
[
'id' => 'project_team',
'name' => 'Team',
'type' => 'group',
'clone' => true,
'sort_clone' => true,
'add_button' => 'Add Member',
'fields' => [
['id' => 'member_name', 'name' => 'Name', 'type' => 'text', 'columns' => 4],
['id' => 'member_role', 'name' => 'Role', 'type' => 'text', 'columns' => 4],
['id' => 'member_photo','name' => 'Photo', 'type' => 'image', 'columns' => 4],
['id' => 'member_hours','name' => 'Hours/wk','type' => 'number','columns' => 3],
],
],
$team = rwmb_meta('project_team', [], $post_id);
foreach ($team as $member) {
echo '<div class="team-member">';
if (!empty($member['member_photo'])) {
$photos = array_values($member['member_photo']);
echo '<img src="' . esc_url($photos[0]['url']) . '">';
}
echo '<strong>' . esc_html($member['member_name']) . '</strong>';
echo '<span>' . esc_html($member['member_role']) . '</span>';
echo '</div>';
}
Conditional Display via visible
[
'id' => 'show_testimonial',
'name' => 'Add Customer Review',
'type' => 'switch',
'style'=> 'rounded',
],
[
'id' => 'testimonial_text',
'name' => 'Review Text',
'type' => 'textarea',
'visible' => ['show_testimonial', '=', true],
],
[
'id' => 'testimonial_author',
'name' => 'Author',
'type' => 'text',
'visible' => ['show_testimonial', '=', true],
'columns' => 6,
],
Fields for Taxonomies and Site Settings
Meta Box supports meta boxes not only for posts:
// Field for taxonomy term
$meta_boxes[] = [
'id' => 'category_color',
'title' => 'Additional',
'taxonomies' => ['project_category'],
'fields' => [
['id' => 'color', 'name' => 'Color', 'type' => 'color'],
['id' => 'icon', 'name' => 'Icon SVG', 'type' => 'textarea', 'rows' => 3],
],
];
// Settings page
$meta_boxes[] = [
'id' => 'site_settings',
'title' => 'Site Settings',
'settings_pages' => ['site-settings'],
'fields' => [
['id' => 'company_phone', 'name' => 'Phone', 'type' => 'text'],
['id' => 'company_email', 'name' => 'Email', 'type' => 'email'],
['id' => 'company_logo', 'name' => 'Logo', 'type' => 'image'],
],
];
Meta Box vs ACF: When to Choose Each
Meta Box is preferable when the project is code-first and needs minimal paid core dependency. ACF PRO is worthwhile if your team is comfortable with its GUI builder and Flexible Content. Performance of both plugins is comparable: both use wp_postmeta, both degrade with thousands of posts with dozens of fields.
For high-traffic projects with large data volumes, both libraries should be replaced with custom tables with optimized indexes.







