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
| Setting | Description |
|---|---|
| Field Key | Unique identifier. Lowercase letters and underscores only. |
| Label | Display label shown above the field. |
| Description | Optional helper text shown below the field. |
| On Text | Label for the “on” state (default: Yes). |
| Off Text | Label for the “off” state (default: No). |
| Default Value | Optional 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 — nottrue/falseor1/0integers - 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