Field Return Types
Every field type returns a specific PHP type when retrieved. This reference covers what to expect from each field so you can write reliable template code.
Quick Reference Table
| Field Type | PHP Type | Example Return Value |
|---|---|---|
| Text | string | "Hello World" |
| Textarea | string | "Line one\nLine two" |
| URL | string | "https://example.com" |
string | "hello@example.com" | |
| Date | string | "2025-12-25" |
| Toggle | string | "1" (on) or "" (off) |
| Select | string | "option_value" |
| Radio | string | "option_value" |
| Button Group | string | "option_value" |
| Checkbox | array | ["value1", "value2"] |
| Image | int | 123 (attachment ID) |
| File | int | 456 (attachment ID) |
| Gallery | array | [123, 456, 789] (attachment IDs) |
| Repeater | array | [["key" => "value"], ...] |
String Fields
Text, Textarea, URL, Email, and Date all return plain strings. Always escape before output:
php
$value = onemeta_get_meta( get_the_ID(), 'field_key' );
echo esc_html( $value ); // For text content
echo esc_url( $value ); // For URLs
echo esc_attr( $value ); // Inside HTML attributes
Toggle Field
Returns "1" when on and "" (empty string) when off. It does not return a boolean.
php
$toggle = onemeta_get_meta( get_the_ID(), 'is_featured' );
// Correct — strict comparison
if ( $toggle === '1' ) {
echo 'Featured';
}
// Incorrect — do not use loose truthiness check
if ( $toggle ) { ... } // This works BUT is misleading — use === '1' for clarity
Choice Fields (Select, Radio, Button Group)
All return the selected choice value as a string — not the label.
php
$status = onemeta_get_meta( get_the_ID(), 'project_status' );
// Returns: "active", "completed", or "archived" — whatever value you set in choices
echo esc_html( $status );
Checkbox Field
Returns an array of selected values. Always check is_array() first.
php
$tags = onemeta_get_meta( get_the_ID(), 'project_tags' );
if ( is_array( $tags ) && ! empty( $tags ) ) {
foreach ( $tags as $tag ) {
echo '<span>' . esc_html( $tag ) . '</span>';
}
}
Image and File Fields
Both return an integer — the WordPress attachment ID.
php
$image_id = onemeta_get_meta( get_the_ID(), 'hero_image' );
if ( ! empty( $image_id ) ) {
echo wp_get_attachment_image( $image_id, 'large' );
}
Gallery Field
Returns an indexed array of attachment IDs (integers).
php
$gallery = onemeta_get_meta( get_the_ID(), 'photo_gallery' );
if ( is_array( $gallery ) && ! empty( $gallery ) ) {
foreach ( $gallery as $image_id ) {
echo wp_get_attachment_image( $image_id, 'medium' );
}
}
Repeater Field
Returns an array of associative arrays — one per row, keyed by sub-field key.
php
$items = onemeta_get_meta( get_the_ID(), 'team_members' );
if ( is_array( $items ) && ! empty( $items ) ) {
foreach ( $items as $item ) {
echo esc_html( $item['name'] ?? '' );
echo esc_html( $item['role'] ?? '' );
}
}
Default Values
All helper functions accept an optional third parameter as a default:
php
// Returns 'Untitled' if hero_title has never been saved
$title = onemeta_get_meta( get_the_ID(), 'hero_title', 'Untitled' );
// Returns [] if gallery has never been saved
$gallery = onemeta_get_meta( get_the_ID(), 'gallery', [] );
The default is only returned when the field has never been saved. A saved empty string returns an empty string — not the default.