Skip to content

Latest commit

 

History

History
74 lines (52 loc) · 1.39 KB

image.mdx

File metadata and controls

74 lines (52 loc) · 1.39 KB

Image

A WordPress media library image picker. Stores the attachment ID.


PHP API

Field::image(string $key): ImageField

Type-specific methods

->returnFormat(string $format): static

Controls what ctrlfield_get() returns. Options:

Format Returns
'id' (default) int — attachment ID
'url' string — full image URL
'array' array — ['id', 'url', 'width', 'height', 'alt']
->previewSize(string $size): static

Registered WordPress image size shown in the editor — thumbnail, medium (default), medium_large, large, full or a theme's own size. It also sets the preview's width.

In the editor, the × on the preview removes the image; Select Image shows only while the field is empty, so a field never holds an image the theme does not read.


Storage

Always stored as an integer attachment ID.

{ "hero_image": 155 }

Example

Field::image('hero_image')
    ->label('Hero Image')
    ->returnFormat('array'),

Template usage

@php $hero = ctrlfield_get('hero_image') @endphp

@if($hero)
    <img
        src="{{ $hero['url'] }}"
        alt="{{ $hero['alt'] }}"
        width="{{ $hero['width'] }}"
        height="{{ $hero['height'] }}"
    >
@endif

With returnFormat('url'):

<img src="{{ ctrlfield_get('logo') }}" alt="Logo">