Skip to main content

Checkbox list

The checkbox list field creates a list of tick-able checkboxes with options. You can select multiple choices from the predefined list.

Screenshots

Settings

Besides the common settings such as Label, ID, Default value, Cloneable, etc., this field has the following specific settings:

specific settings of the field

NameKeyDescription
ChoicesoptionsList of choices, each per line. If you need to set values and labels, use the format "value: Label" for each choice.
When using code, this setting is an array of 'value' => 'Label'.
InlineinlineDisplay choices in a single line? true or false.
Display "Toggle All" buttonselect_all_noneDisplay "Toggle All" button to quickly toggle choices.
note
  • The keys are for using with code.
  • The interface of field settings just comes when you have the Meta Box Builder extension in your pocket. It’s a premium extension providing the UI to create and configure fields visually. It’s already bundled in the Meta Box AIO and MB Core.

When creating the field with Meta Box Builder, just input options into the Choices box.

Input the label of the options in the Choices box

In some special cases, you may want the data stored (value) to be different from the data displayed (label), you can input data with the form value:label.

the field with settings and options

This is the field settings array when creating this field with code:

[
'name' => 'Checkbox list',
'id' => 'field_id',
'type' => 'checkbox_list',
'inline' => true,
'select_all_none' => true,
'options' => [
'java' => 'Java',
'javascript' => 'JavaScript',
'php' => 'PHP',
'csharp' => 'C#',
'kotlin' => 'Kotlin',
'swift' => 'Swift',
],
],

In the post/page editor, the checkbox list will display with options.

The field displays in the post/page editor

Data

This field saves multiple values in the database. Each value is stored in a single row in the database with the same key (similar to what add_post_meta does with the last parameter false).

If the field is cloneable, then the value is stored as a serialized array in a single row in the database. Each value of that array is an array of cloned values.

caution

Note that this field stores the values, not labels.

Template usage

Using MB Views

MB Views is an extension for Meta Box, which helps you to get Meta Box fields and build your templates on the front end fast and easily. The extension supports all custom fields built with Meta Box, and also post fields (such as post title and post content), site settings, user fields, and even query fields. MB Views is already bundled in the Meta Box AIO. So it's available in the Ultimate and Lifetime plans.

Displaying selected values or labels:

In the view, click on the Insert Field button.

Go to Views and click on the Insert Field button

Find the name of the created field on the list on the right sidebar.

Find the name of the created field on the list on the right sidebar

There will be two options to output the data, they are the value and label of the choices that we input in the field.

Just choose one to output only the values, or only the labels. In the event that you want to display both of them, just insert the field twice.

choose one option to output only the values, or only the labels

Then the code will be like this:

The code to display selected values

Display only the labels:

Display only the labels

Display both values and labels:

Display both values and labels

Displaying cloneable values:

No matter if the field is cloneable or not, the operation to display data from the field will be the same. The difference is just in the generated code.

Displaying cloneable values

Using PHP

Displaying selected values:

<?php $values = rwmb_meta( 'my_field_id' ); ?>
<ul>
<?php foreach ( $values as $value ) : ?>
<li><?= $value ?></li>
<?php endforeach ?>
</ul>

Displaying selected labels:

<p>Choices:</p>
<?php rwmb_the_value( 'my_field_id' ) ?>
info

rwmb_the_value() automatically formats values as an unordered list.

Displaying both values and labels:

<?php
$field = rwmb_get_field_settings( 'my_field_id' );
$options = $field['options'];
$values = rwmb_meta( 'my_field_id' );
?>
<ul>
<?php foreach ( $values as $value ) : ?>
<li>
Value: <?= $value ?><br>
Label: <?= $options[ $value ] ?>
</li>
<?php endforeach ?>
</ul>

Displaying cloneable values:

<?php
$field = rwmb_get_field_settings( 'my_field_id' );
$options = $field['options'];
$values = rwmb_meta( 'my_field_id' );
?>
<ul>
<?php foreach ( $values as $clone ) : ?>
<li>
<ul>
<?php foreach ( $clone as $value ) : ?>
<li>
Value: <?= $value ?><br>
Label: <?= $options[ $value ] ?>
</li>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>