Skip to content

Image

An image uploader that integrates with the WordPress media library and returns an attachment ID.

Image Field

The Image field opens the WordPress media library, allowing the user to select or upload an image. It stores the attachment ID — not a URL — so you have full control over image size and output.


Field Settings

SettingDescription
Field KeyUnique identifier. Lowercase letters and underscores only.
LabelDisplay label shown above the field.
DescriptionOptional helper text shown below the field.
InstructionsOptional instructions shown above the upload button.

Return Value

Returns an integer — the WordPress attachment ID. Returns an empty string if no image is selected.

Always check ! empty() before using the ID, as an empty string is falsy but 0 is also falsy.


Usage

php

// Get the attachment ID
$image_id = onemeta_get_meta( get_the_ID(), 'field_key' );

if ( ! empty( $image_id ) ) {
    // Recommended: use wp_get_attachment_image() for full srcset, lazy loading, alt text
    echo wp_get_attachment_image( $image_id, 'large', false, [
        'class' => 'featured-image',
        'alt'   => get_the_title(),
    ] );

    // Or get just the URL
    $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    echo '<img src="' . esc_url( $image_url ) . '" alt="">';
}

Template Example

php

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

<?php if ( ! empty( $banner_id ) ) : ?>
    <figure class="page-banner">
        <?php echo wp_get_attachment_image( $banner_id, 'full', false, [
            'class' => 'page-banner__image',
            'alt'   => esc_attr( get_the_title() ),
        ] ); ?>
    </figure>
<?php endif; ?>

Notes

  • The field stores and returns an attachment ID (integer), not a URL
  • Use wp_get_attachment_image() — it automatically handles srcset, sizes, lazy loading, and alt text
  • Use wp_get_attachment_image_url( $id, $size ) if you only need the URL

On This Page