Skip to content

Toggle

An on/off switch for boolean values like featured status, visibility, or any true/false setting.

Toggle Field

The Toggle field is an on/off switch for boolean values. Use it for featured status, visibility flags, enable/disable settings, or any field that has exactly two states.


Field Settings

SettingDescription
Field KeyUnique identifier. Lowercase letters and underscores only.
LabelDisplay label shown above the field.
DescriptionOptional helper text shown below the field.
On TextLabel for the “on” state (default: Yes).
Off TextLabel for the “off” state (default: No).
Default ValueOptional default state.

Return Value

Returns a string"1" when on, "" (empty string) when off.

Important: The Toggle field does not return a boolean. Always compare with === '1' rather than using a loose truthiness check, as "0" is also truthy in PHP.


Usage

php

// Get the value
$is_featured = onemeta_get_meta( get_the_ID(), 'field_key' );

// Check if enabled — use === '1', not just if ($value)
if ( $is_featured === '1' ) {
    echo '<span class="badge">Featured</span>';
}

Template Example

php

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

<?php if ( $is_featured === '1' ) : ?>
    <div class="featured-ribbon">⭐ Featured</div>
<?php endif; ?>

Notes

  • Returns "1" for on and "" for off — not true/false or 1/0 integers
  • Always use strict comparison === '1' to check the on state
  • The On Text and Off Text settings change the label displayed in the admin — they do not affect the stored value

On This Page