Support Forum
Hi there,
Me again...lol...but no need to worry I have a very simple question here regarding the order of fields, specifically checkboxes.
I created a metabox that initially has one check box.
I am creating a plugin that will add an additional six (6) checkboxes to that metabox once activated.
I used this https://docs.metabox.io/edit-meta-boxes/ to add the additional checkboxes. (Great tutorial which was pretty easy...thank you.)
Now what I would like to do is put those checkboxes in a specific order. Is there a way that can be done?
I've looked at all the documentation and can't find anything, so I am curious if this can be done.
I appreciate any information on the topic.
Thank you in advance for all you do.
Hi Thomas,
Thanks for a nice question. Yes, it's doable. In the code from the tutorial, notice this part:
add_filter( 'rwmb_meta_boxes', 'prefix_edit_meta_boxes', 20 );
The last number is the priority of the code to add fields to a meta box. The plugin which has lower number will run first. So, if you have 6 plugins, just set different numbers for them in a order you want, like this:
add_filter( 'rwmb_meta_boxes', 'prefix_edit_meta_boxes_1', 20 );
add_filter( 'rwmb_meta_boxes', 'prefix_edit_meta_boxes_2', 30 );
add_filter( 'rwmb_meta_boxes', 'prefix_edit_meta_boxes_3', 40 );
Hi there,
Thank you for the reply. Sorry but my question was not how to set the order of metaboxes but the order of fields within a metabox. This is what I am using:
function tp_primo_edit_meta_boxes( $meta_boxes ) {
// Loop throught all meta boxes to find the ones we need to change
foreach ( $meta_boxes as $tp_primo_mb => $meta_box ) {
// find the metabox we want to modify
if ( isset( $meta_box['id'] ) && 'totalpress-post-page-options' == $meta_box['id'] ) {
// then add the fields we want/need to our tab
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'totalpress_featured_image_layout_options',
'type' => 'radio',
'options' => array (
'page_header_contained' => esc_html__('Page Header Contained','tp-primo'),
'page_header_contained_title_overlay' => esc_html__('Page Header Cont. w/Title','tp-primo'),
'page_header_contained_title_overlay_parallax' => esc_html__('Page Header Cont. w/Title - Parallax','tp-primo'),
'page_header_full_width' => esc_html__('Page Header Full WIdth','tp-primo'),
'page_header_full_width_title_overlay' => esc_html__('Page Header FW w/Title','tp-primo'),
'page_header_full_width_title_overlay_parallax' => esc_html__('Page Header FW w/Title - Parallax','tp-primo'),
),
'tab' => 'totalpress_featured_image_options',
'inline' => false,
'std' => array (
'show_below_title',
),
);
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_header_height',
'type' => 'text',
'name' => esc_html__('Page Header Height','tp-primo'),
'desc' => esc_html__('Use PX, %, REM, EM and VH based values.','tp-primo'),
'tab' => 'totalpress_featured_image_options',
);
}
// find the metabox we want to modify
if ( isset( $meta_box['id'] ) && 'totalpress-post-page-options' == $meta_box['id'] ) {
// then add the fields we want/need to our tab
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_set_content_area_width',
'type' => 'checkbox',
'desc' => esc_html__('Set Content Area to 100% Width','tp-primo'),
'tab' => 'totalpress_page_builder_options',
);
}
// find the metabox we want to modify
if ( isset( $meta_box['id'] ) && 'totalpress-post-page-options' == $meta_box['id'] ) {
// then add the fields we want/need
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_options_hide_top_sidebar',
'type' => 'checkbox',
'desc' => esc_html__('Hide Post/Page Top Sidebar','tp-primo'),
'tab' => 'totalpress_hide_post_page_elements',
);
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_options_hide_header',
'type' => 'checkbox',
'desc' => esc_html__('Hide Post/Page Theme Header','tp-primo'),
'tab' => 'totalpress_hide_post_page_elements',
);
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_options_hide_navigation',
'type' => 'checkbox',
'desc' => esc_html__('Hide Post/Page Main Navigation','tp-primo'),
'tab' => 'totalpress_hide_post_page_elements',
);
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_options_hide_featured_image',
'type' => 'checkbox',
'desc' => esc_html__('Hide Post/Page Featured Image','tp-primo'),
'tab' => 'totalpress_hide_post_page_elements',
);
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_options_hide_footer_widgets',
'type' => 'checkbox',
'desc' => esc_html__('Hide All Post/Page Footer Widgets','tp-primo'),
'tab' => 'totalpress_hide_post_page_elements',
);
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_options_hide_footer',
'type' => 'checkbox',
'desc' => esc_html__('Hide Post/Page Theme Footer','tp-primo'),
'tab' => 'totalpress_hide_post_page_elements',
);
}
}
return $meta_boxes;
}
add_filter('rwmb_meta_boxes','tp_primo_edit_meta_boxes', 20);
I was referring to the last section of that code totalpress-post-page-options
- there is a total of 6 checkboxes being added to that one specific metabox.
My question was how to set the order of those checkboxes without having to create a function for each one.
I hope that made sense.
Hi Thomas,
I got it. The easiest way to order those fields is just order the code for them. The code that write first will display first.
Let's say you want to move "Hide Post/Page Theme Header" to the top of the list, then just move this code to the top
if ( isset( $meta_box['id'] ) && 'totalpress-post-page-options' == $meta_box['id'] ) {
$meta_boxes[$tp_primo_mb]['fields'][] = array(
'id' => 'tp_primo_page_options_hide_header',
'type' => 'checkbox',
'desc' => esc_html__('Hide Post/Page Theme Header','tp-primo'),
'tab' => 'totalpress_hide_post_page_elements',
);
// Code for other checkboxes goes here
}
Hi there,
This is what I thought, however this will not work for me. See, in my theme one checkbox is available for the user. It is called "Hide Post/Page Title".
So when they install the plugin, that is when more checkboxes become available (the ones above). I just wanted to see if I could put them in order. If I can't...oh well....lol.
But thanks for trying anyway. I really do appreciate it.