Skip to content

Checkbox

A multiple-selection field using checkboxes, returning an array of selected values.

Checkbox Field

The Checkbox field allows selecting multiple options simultaneously. Use it for feature lists, amenities, tags, skills, or any field where more than one option can be chosen at once.


Field Settings

SettingDescription
Field KeyUnique identifier. Lowercase letters and underscores only.
LabelDisplay label shown above the field.
DescriptionOptional helper text shown below the field.
ChoicesList of checkable options. Each has a value and a label.
Default ValueOptional pre-checked choices.
Layoutvertical or horizontal layout for the checkboxes.

Return Value

Returns an array of selected values. Returns an empty array [] if nothing is selected.

Always check is_array() before looping — never assume the return is an array without checking.


Usage

php

// Get the value (returns array)
$features = onemeta_get_meta( get_the_ID(), 'field_key' );

// Loop through selected values
if ( is_array( $features ) && ! empty( $features ) ) {
    echo '<ul>';
    foreach ( $features as $feature ) {
        echo '<li>' . esc_html( $feature ) . '</li>';
    }
    echo '</ul>';
}

// Check if a specific value is selected
if ( is_array( $features ) && in_array( 'wifi', $features, true ) ) {
    echo '📶 WiFi Available';
}

Template Example

php

<?php $amenities = onemeta_get_meta( get_the_ID(), 'amenities' ); ?>

<?php if ( is_array( $amenities ) && ! empty( $amenities ) ) : ?>
    <ul class="amenities-list">
        <?php foreach ( $amenities as $amenity ) : ?>
            <li class="amenity-item">
                <?php echo esc_html( $amenity ); ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Notes

  • Always use is_array() before looping — the value may be empty or malformed
  • Use in_array( $value, $array, true ) with strict comparison to check for a specific selection
  • The returned values are the choice values, not the labels

On This Page