Developer documentation

Theme integration

A complete guide to the 20 public helpers in version 1.19.0. Keep one attachment in WordPress or ACF and resolve the generated files when rendering your theme.

01

Quick start

Use these examples in a theme template or a child theme. The variables $imageId, $videoId and $attachmentId are existing WordPress attachment IDs. Helpers return markup or data and never run a conversion during a page request.

Guard your integration with function_exists() so the theme also works when the plugin is deactivated. Optimization must be enabled and the queue must finish before modern sources are available.
PHP · Example 1
<?php
$imageId = (int) get_post_thumbnail_id();

if ($imageId > 0) {
    echo function_exists('akita_media_picture')
        ? akita_media_picture($imageId, ['profile' => 'hero'])
        : wp_get_attachment_image($imageId, 'large');
}

02

Responsive images

akita_media_picture() returns AVIF/WebP source elements and a native WordPress image as fallback. akita_media_get() returns the data for custom rendering. A missing derivative keeps the original image available.

PHP · Example 1
<?php
echo akita_media_picture($imageId, [
    'size' => 'large',
    'formats' => ['avif', 'webp'],
    'sizes' => '(max-width: 768px) 100vw, 1200px',
    'class' => 'hero__image',
    'picture_class' => 'hero__picture',
    'alt' => get_post_meta($imageId, '_wp_attachment_image_alt', true),
    'loading' => 'eager',
    'fetchpriority' => 'high',
    'decoding' => 'async',
    'attributes' => ['data-media' => 'hero'],
]);

$image = akita_media_get($imageId, ['profile' => 'card']);
$fallbackUrl = $image['fallback']['url'] ?? '';
$webpSrcset = $image['sources']['webp']['srcset'] ?? '';
  • The resolved array contains attachment_id, source_attachment_id, optimized, fallback, sources, status, error and profile.
  • fallback contains url, width, height, srcset, sizes, mime_type and filesize; it can be null. sources is keyed by format and includes srcset, assets and mime_type.
  • No valid attachment or fallback: the renderer returns an empty string. Existing but not yet optimized images return a normal WordPress img.

Available options

ParameterBehavior and return value
profileRegistered profile name; optional. Explicit delivery options take precedence.
sizeWordPress size name or [width, height]. Default: full.
formatsOrdered list of modern formats. Default: ['avif', 'webp'].
sizesResponsive layout sizes string, otherwise calculated by WordPress or taken from the profile.
class / picture_classClasses on img / picture. picture exists only when modern sources are available.
altAlternative text. Without an override, WordPress attachment metadata is used.
loading / fetchpriority / decodinglazy|eager; high|low|auto; async|sync|auto. A profile can supply defaults.
attributesAdditional allowed attributes such as id, title, data-* and aria-*. Event handlers and raw src overrides are not accepted.

03

Built-in and custom profiles

A profile combines image widths, cropping and delivery attributes. Register custom profiles during after_setup_theme at priority 10, before the plugin registers WordPress image sizes at priority 20.

New profile sizes do not retroactively appear on old uploads. Regenerate their WordPress thumbnails, then scan and enqueue the media library again. Rendering a profile does not create files.
ProfileWidths, pxBehavior and return value
hero768, 1280, 192016:9 crop, eager, high priority, sizes=100vw.
card320, 640, 9604:3 crop, lazy, auto priority, 100vw / 33vw.
gallery480, 960, 1440Original proportions, lazy, auto priority, 100vw / 50vw.
PHP · Example 1
<?php
add_action('after_setup_theme', function (): void {
    if (! function_exists('akita_media_register_profile')) {
        return;
    }

    akita_media_register_profile('case-study', [
        'widths' => [600, 1200, 1800],
        'aspect_ratio' => '3:2',
        'crop' => ['center', 'top'],
        'formats' => ['avif', 'webp'],
        'sizes' => '(max-width: 768px) 100vw, 1200px',
        'loading' => 'lazy',
        'fetchpriority' => 'auto',
        'decoding' => 'async',
        'class' => 'case-study__image',
        'picture_class' => 'case-study__picture',
    ]);
}, 10);
PHP · Example 2
<?php
$profiles = akita_media_profiles();
echo akita_media_picture($imageId, ['profile' => 'case-study']);
  • widths accepts unique integers from 64 to 8192. aspect_ratio accepts W:H, [W, H] or null.
  • crop accepts true, false or [left|center|right, top|center|bottom]. With no aspect ratio, the source proportions are preserved.
  • An unknown profile reports unknown_profile in resolved data and uses normal image delivery. Per-call options override profile delivery defaults.

Available options

ParameterBehavior and return value
widthsRequired widths, 64–8192 pixels.
aspect_ratio / cropW:H or [W, H]; null preserves proportions. Crop positions support center/top and the other documented directions.
formats / sizesGenerated formats and responsive delivery defaults.
class / picture_class / loading / fetchpriority / decodingImage delivery defaults, overridden by per-call arguments.

04

ACF images, galleries and files

The resolver accepts an ID, numeric string, attachment array (ID, id, attachment_id or url), WP_Post, local attachment URL or a list of those values. It preserves input order and removes duplicate IDs.

Use URLs belonging to the WordPress media library. External URLs are not downloaded or converted. Prefer the ID return format for new ACF fields.
PHP · Example 1
<?php
if (function_exists('get_field') && function_exists('akita_media_resolve_acf')) {
    $image = akita_media_resolve_acf(get_field('hero_image'));
    foreach ($image['attachment_ids'] as $imageId) {
        echo akita_media_picture($imageId, ['profile' => 'hero']);
    }

    $gallery = akita_media_resolve_acf(get_field('gallery'));
    foreach ($gallery['attachment_ids'] as $imageId) {
        echo akita_media_picture($imageId, ['profile' => 'gallery']);
    }

    $video = akita_media_resolve_acf(get_field('video_file'));
    foreach ($video['items'] as $item) {
        if ($item['media_type'] === 'video') {
            echo akita_media_video($item['attachment_id']);
        }
    }
}
PHP · Example 2
<?php
$files = akita_media_resolve_acf(get_field('download_file'));
foreach ($files['items'] as $file) {
    if ($file['media_type'] === 'file' && $file['url'] !== '') {
        printf('<a href="%s">%s</a>', esc_url($file['url']), esc_html($file['title']));
    }
}
  • The result contains multiple, attachment_ids, items and error. No valid media returns empty lists and attachment_not_found.
  • Images expose the responsive image data, videos expose url, webm_url, sources and poster_url. Other files keep their original URL and optimized=false.
  • Translated attachments sharing one physical source reuse generated files while retaining translated title and alt. A different physical file is processed separately.

05

Video sources and posters

akita_media_video() renders video with verified WebM first and MP4 or the available source as fallback. The sources helper lets your theme own the markup. Posters are generated at the position selected in plugin settings.

Before conversion completes, the fallback may be the original upload, including a format the browser cannot play. Check processing and codec diagnostics before publishing. WebM generation is optional.
PHP · Example 1
<?php
echo akita_media_video($videoId, [
    'class' => 'project-video',
    'controls' => true,
    'playsinline' => true,
    'preload' => 'metadata',
    'width' => 1280,
    'height' => 720,
]);
PHP · Example 2
<video controls playsinline preload="metadata"
    poster="<?= esc_url(akita_media_poster_url($videoId, 'large')); ?>">
    <?php foreach (akita_media_video_sources($videoId) as $source) { ?>
        <source src="<?= esc_url($source['url']); ?>"
            type="<?= esc_attr($source['type']); ?>">
    <?php } ?>
</video>
PHP · Example 3
<?php
$deliveryId = akita_media_video_id($videoId);
$deliveryUrl = akita_media_video_url($videoId);
$webmId = akita_media_video_webm_id($videoId);
$webmUrl = akita_media_video_webm_url($videoId);
$posterId = akita_media_poster_id($videoId);
$posterUrl = akita_media_poster_url($videoId, 'large');
  • The sources result is a list of {url, type}. Missing WebM or poster returns 0 for its ID and an empty string for its URL.
  • The video URL and ID helpers return the generated MP4 when available, otherwise the original attachment. They do not force conversion.
  • The renderer includes a generated poster automatically. Pass poster='' to omit it or a URL to override it. Escape URLs and attributes when writing your own markup.

Available options

ParameterBehavior and return value
posterGenerated poster by default, custom URL to override, empty string to omit.
preloadnone, metadata (default) or auto.
autoplay / controls / loop / muted / playsinlineBoolean flags. controls and playsinline default to true. The other flags default to false. Autoplay usually also needs muted.
id / class / aria-labelSupported identifying and accessibility attributes.
width / heightPositive integer dimensions. Do not imply transcoding or resizing.

06

CSS backgrounds

background_style() returns an original background-image declaration followed by image-set() when verified modern sources exist. Escape the complete declaration with esc_attr() in an HTML style attribute.

background_url() defaults to the original format. Request webp or avif only when your integration controls browser support. Missing requested formats fall back to the original. Background images do not provide alternative text.
PHP · Example 1
<section class="page-hero"
    style="<?= esc_attr(akita_media_background_style($imageId, ['profile' => 'hero'])); ?>">
    ...
</section>
PHP · Example 2
<?php
$originalUrl = akita_media_background_url($imageId, ['size' => 'large']);
$webpUrl = akita_media_background_url($imageId, [
    'size' => 'large',
    'format' => 'webp',
]);

07

Automatic WordPress delivery

Opt in from the plugin settings or use the integration helper for wp_get_attachment_image() and core/image blocks. It preserves the native img element and does not wrap existing picture markup twice.

This is not a rewrite of every image URL on the website. Custom HTML, CSS backgrounds and third-party page builders may require explicit helpers.
PHP · Example 1
<?php
add_action('after_setup_theme', function (): void {
    if (! function_exists('akita_media_enable_content_integration')) {
        return;
    }

    akita_media_enable_content_integration([
        'attachment_images' => true,
        'core_image_blocks' => true,
        'formats' => ['avif', 'webp'],
        'picture_class' => 'site-picture',
        'include' => static fn (int $id, string $context, array $args): bool => ! is_feed(),
    ]);
}, 30);
PHP · Example 2
<?php
add_action('wp', function (): void {
    if (is_page_template('templates/raw-media.php')
        && function_exists('akita_media_disable_content_integration')) {
        akita_media_disable_content_integration();
    }
});
  • The include callback receives attachment ID, context and arguments. Context is attachment_image or core_image_block.
  • Disable integration for the current request with akita_media_disable_content_integration(). It does not change saved settings.

Available options

ParameterBehavior and return value
attachment_images / core_image_blocksEnable each integration separately; both default to true.
formats / profile / picture_classModern format order, optional profile and one picture class token.
includeOptional callable (int $id, string $context, array $args): bool.

08

Source identity and readiness

source_id() resolves the physical source used by WPML. status() reads its stored image/video processing status. is_ready() is true only when that status equals ready.

Do not hide media while waiting for ready. Render with the helpers and let them provide the fallback. A ready job is not a promise that every optional output format exists.
PHP · Example 1
<?php
$sourceId = akita_media_source_id($attachmentId);
$status = akita_media_status($attachmentId);
$ready = akita_media_is_ready($attachmentId);

echo akita_media_picture($imageId, ['profile' => 'card']);

09

Delivery filters

Use filters for theme-specific policies. Return an array from akita_media_resolved_image, a string from akita_media_picture_html and an array or null from akita_media_auto_delivery_args.

PHP · Example 1
<?php
add_filter('akita_media_auto_delivery_args', function (
    array $args,
    int $attachmentId,
    string $context,
    array $contextData
): ?array {
    if (is_feed()) {
        return null;
    }

    $args['formats'] = ['webp'];

    return $args;
}, 10, 4);
PHP · Example 2
<?php
add_filter('akita_media_resolved_image', function (
    array $image,
    int $attachmentId,
    array $args
): array {
    $image['theme_context'] = $args['profile'] ?? null;

    return $image;
}, 10, 3);

add_filter('akita_media_picture_html', function (
    string $html,
    array $image,
    int $attachmentId,
    array $args
): string {
    return str_replace('<picture', '<picture data-theme="project"', $html);
}, 10, 4);
  • auto_delivery_args receives arguments, attachment ID, context and context data. Return null to leave the original markup unchanged.
  • resolved_image receives the data array, attachment ID and normalized arguments. Preserve its existing keys.
  • picture_html receives the HTML string, resolved image, attachment ID and arguments. It runs when optimized picture markup is available.

10

All 20 public functions

Each entry shows the public signature, return behavior and a minimal call. Use the surrounding integration examples for lifecycle timing, HTML escaping and plugin guards.

akita_media_get(int $attachmentId, array $args = []): array
Normalized image data array; fallback can be null and sources can be empty.
$image = akita_media_get($imageId, ['size' => 'large']);
akita_media_picture(int $attachmentId, array $args = []): string
Rendered picture or native WordPress img; empty string if no image can be rendered.
echo akita_media_picture($imageId, ['profile' => 'hero']);
akita_media_register_profile(string $name, array $config): bool
Registers a profile and returns true. Invalid widths or ratios throw InvalidArgumentException.
akita_media_register_profile('square', ['widths' => [320, 640], 'aspect_ratio' => '1:1']);
akita_media_profiles(): array
All registered profile definitions, keyed by name.
$hero = akita_media_profiles()['hero'] ?? null;
akita_media_resolve_acf(mixed $value, array $args = []): array
Normalized collection with multiple, attachment_ids, items and error.
$gallery = akita_media_resolve_acf(get_field('gallery'), ['profile' => 'gallery']);
akita_media_enable_content_integration(array $config = []): bool
Enables WordPress image/block filters for this request and returns true.
akita_media_enable_content_integration(['picture_class' => 'site-picture']);
akita_media_disable_content_integration(): void
Removes automatic image/block filters for this request.
akita_media_disable_content_integration();
akita_media_background_style(int $attachmentId, array $args = []): string
CSS declaration with original fallback and optional image-set(); empty string without a fallback.
$css = akita_media_background_style($imageId, ['profile' => 'hero']);
akita_media_background_url(int $attachmentId, array $args = []): string
Requested background format URL, or original fallback. Default format: original.
$url = akita_media_background_url($imageId, ['format' => 'webp']);
akita_media_source_id(int $attachmentId): int
Canonical physical source attachment ID with WPML-aware mapping.
$sourceId = akita_media_source_id($attachmentId);
akita_media_video_id(int $attachmentId): int
Generated MP4 attachment ID if available, otherwise the requested attachment ID.
$deliveryId = akita_media_video_id($videoId);
akita_media_video_url(int $attachmentId): string
URL of the generated MP4 or original source, or an empty string.
$url = akita_media_video_url($videoId);
akita_media_video_webm_id(int $attachmentId): int
Generated WebM attachment ID, or 0.
$webmId = akita_media_video_webm_id($videoId);
akita_media_video_webm_url(int $attachmentId): string
Generated WebM URL, or an empty string.
$webmUrl = akita_media_video_webm_url($videoId);
akita_media_video_sources(int $attachmentId): array
Ordered list of {url, type}, WebM before the available fallback; empty if no URLs exist.
$sources = akita_media_video_sources($videoId);
akita_media_video(int $attachmentId, array $args = []): string
Rendered video/source markup with optional poster, or an empty string.
echo akita_media_video($videoId, ['controls' => true]);
akita_media_poster_id(int $attachmentId): int
Generated poster attachment ID, or 0.
$posterId = akita_media_poster_id($videoId);
akita_media_poster_url(int $attachmentId, string $size = 'full'): string
Generated poster URL in a named WordPress size, or an empty string.
$posterUrl = akita_media_poster_url($videoId, 'large');
akita_media_status(int $attachmentId): string
Stored image/video processing status of the source, or an empty string.
$status = akita_media_status($attachmentId);
akita_media_is_ready(int $attachmentId): bool
True only when the source processing status is ready.
$ready = akita_media_is_ready($attachmentId);

11

Checks and troubleshooting

Confirm both generation and delivery on a real page. A generated file does not help if the theme continues to output an unrelated original URL.

  • Open Site Health and check image/video encoders and queue schedules. SSH is not required for the standard WP-Cron workflow.
  • For old uploads, regenerate missing WordPress sizes if profiles changed, then scan and enqueue the library. Wait for jobs to finish.
  • Inspect the rendered picture/source markup and the browser network panel. A fallback is expected when a format is missing or stale.
  • After changing policy, regenerate media and clear the relevant page/CDN cache. Profile registration alone does not rebuild cached pages.
  • Keep gallery images lazy, and use eager/high priority only for the actual leading image. A sizes attribute describes layout, not a new output size.