01
Schnellstart
Die Beispiele gehören in ein Theme-Template oder Child-Theme. $imageId, $videoId und $attachmentId sind vorhandene WordPress-Anhang-IDs. Die Helfer liefern Daten oder HTML und konvertieren beim Seitenaufruf keine Dateien.
<?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 Bilder
picture() liefert AVIF/WebP-Quellen und das WordPress-img als Fallback. get() enthält attachment_id, source_attachment_id, optimized, fallback, sources, status, error und profile. sources ist nach Format indiziert.
<?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'] ?? '';
Verfügbare Optionen
| Parameter | Verhalten und Rückgabe |
|---|---|
profile | Profilname. Explizite Aufrufoptionen haben Vorrang. |
size | WordPress-Größenname oder [Breite, Höhe]. Standard: full. |
formats | Formatreihenfolge, standardmäßig ['avif', 'webp']. |
sizes | Responsive Layoutbreite, sonst Profil- oder WordPress-Wert. |
class / picture_class | Klassen für img/picture. picture entsteht nur mit modernen Quellen. |
alt | Alternativtext, sonst WordPress-Anhang-Metadaten. |
loading / fetchpriority / decoding | lazy|eager; high|low|auto; async|sync|auto. Profile liefern Standardwerte. |
attributes | Erlaubte Attribute wie id, title, data-* und aria-*. Keine Event-Handler oder src-Überschreibung. |
03
Standardprofile und eigene Profile
Ein Profil verbindet Breiten, Zuschnitt und Ausgabeattribute. Registrieren Sie es in after_setup_theme mit Priorität 10, vor der Größenregistrierung des Plugins bei Priorität 20.
| Profil | Breiten, px | Verhalten und Rückgabe |
|---|---|---|
hero | 768, 1280, 1920 | 16:9-Zuschnitt, eager, hohe Priorität, sizes=100vw. |
card | 320, 640, 960 | 4:3-Zuschnitt, lazy, Priorität auto, 100vw / 33vw. |
gallery | 480, 960, 1440 | Originalproportionen, lazy, Priorität auto, 100vw / 50vw. |
<?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
$profiles = akita_media_profiles();
echo akita_media_picture($imageId, ['profile' => 'case-study']);
- widths: eindeutige Ganzzahlen von 64 bis 8192. aspect_ratio: W:H, [W, H] oder null.
- crop: true, false oder [left|center|right, top|center|bottom]. Ohne Seitenverhältnis bleiben die Quellproportionen erhalten.
- Explizite Argumente überschreiben das Profil. Unbekannte Profile melden unknown_profile und verwenden die normale Bildausgabe.
Verfügbare Optionen
| Parameter | Verhalten und Rückgabe |
|---|---|
widths | Pflichtliste mit Breiten von 64–8192 px. |
aspect_ratio / crop | W:H oder [W, H]; null erhält Proportionen. Positionsbasierter Zuschnitt möglich. |
formats / sizes | Formate und responsive Standardwerte. |
class / picture_class / loading / fetchpriority / decoding | Ausgabeattribute, die pro Aufruf überschrieben werden können. |
04
ACF: Bilder, Galerien und Dateien
Der Resolver akzeptiert ID, numerischen String, Anhang-Array mit ID/id/attachment_id/url, WP_Post, lokale Anhang-URL oder eine Liste. Reihenfolge bleibt erhalten, doppelte IDs werden entfernt.
<?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
$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']));
}
}
- Rückgabe: multiple, attachment_ids, items und error. Ohne Treffer: leere Listen und attachment_not_found.
- Bilder liefern responsive Daten, Videos url/webm_url/sources/poster_url. Andere Dateien behalten das Original und optimized=false.
- WPML-Übersetzungen derselben physischen Datei teilen Derivate und behalten übersetzte title- und alt-Werte.
05
Videoquellen und Poster
video() erzeugt video mit geprüftem WebM vor MP4 oder der verfügbaren Originalquelle. video_sources() liefert {url, type} für eigenes HTML. Die Posterposition stammt aus den Plugin-Einstellungen.
<?php
echo akita_media_video($videoId, [
'class' => 'project-video',
'controls' => true,
'playsinline' => true,
'preload' => 'metadata',
'width' => 1280,
'height' => 720,
]);
<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
$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');
- video_id/url verwenden erzeugtes MP4 oder Original. Sie starten keine Konvertierung.
- Fehlendes WebM/Poster: ID=0, URL ist leer. poster='' lässt das Poster weg; eine URL ersetzt es.
- Eigenes HTML mit esc_url() und esc_attr() absichern.
Verfügbare Optionen
| Parameter | Verhalten und Rückgabe |
|---|---|
poster | Erzeugtes Poster als Standard, eigene URL ersetzt, leerer String entfernt es. |
preload | none, metadata (Standard) oder auto. |
autoplay / controls / loop / muted / playsinline | Boolesche Werte. controls/playsinline standardmäßig true, andere false. Autoplay benötigt üblicherweise muted. |
id / class / aria-label | Unterstützte Kennzeichnungs- und Barrierefreiheitsattribute. |
width / height | Positive ganze Abmessungen. Starten keine Konvertierung. |
06
CSS-Hintergründe
background_style() liefert original background-image und bei verfügbaren Derivaten image-set(). Das vollständige style-Attribut mit esc_attr() absichern.
<section class="page-hero"
style="<?= esc_attr(akita_media_background_style($imageId, ['profile' => 'hero'])); ?>">
...
</section>
<?php
$originalUrl = akita_media_background_url($imageId, ['size' => 'large']);
$webpUrl = akita_media_background_url($imageId, [
'size' => 'large',
'format' => 'webp',
]);
07
Automatische WordPress-Ausgabe
Aktivieren Sie die Integration in den Einstellungen oder per Helfer für wp_get_attachment_image() und core/image. Das vorhandene img bleibt erhalten; picture wird nicht doppelt umschlossen.
<?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
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();
}
});
- include erhält Anhang-ID, Kontext (attachment_image/core_image_block) und Argumente.
- disable_content_integration() entfernt Filter nur für die aktuelle Anfrage und ändert keine gespeicherten Einstellungen.
Verfügbare Optionen
| Parameter | Verhalten und Rückgabe |
|---|---|
attachment_images / core_image_blocks | Separate Schalter, beide standardmäßig true. |
formats / profile / picture_class | Formatreihenfolge, optionales Profil und ein picture-Klassentoken. |
include | Optionaler Callback (int $id, string $context, array $args): bool. |
08
Quelle und Bereitschaft
source_id() ermittelt die physische Quelle mit WPML-Zuordnung. status() liest ihren gespeicherten Status. is_ready() ist nur bei ready wahr.
<?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
Filter für die Ausgabe
resolved_image erhält Daten, ID und Argumente und muss ein Array liefern. picture_html erhält HTML, Daten, ID und Argumente und liefert einen String. auto_delivery_args erhält Argumente, ID, Kontext und Kontextdaten.
<?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
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);
10
Alle 20 öffentlichen Funktionen
Jeder Eintrag zeigt Signatur, Rückgabe und einen Beispielaufruf. Lebenszyklus, Plugin-Prüfung und HTML-Escaping stehen in den zugehörigen Abschnitten.
akita_media_get(int $attachmentId, array $args = []): array- Bilddaten; fallback kann null und sources leer sein.
$image = akita_media_get($imageId, ['size' => 'large']);akita_media_picture(int $attachmentId, array $args = []): string- picture oder WordPress-img; leerer String ohne darstellbares Bild.
echo akita_media_picture($imageId, ['profile' => 'hero']);akita_media_register_profile(string $name, array $config): bool- Registriert ein Profil, liefert true. Ungültige Werte werfen InvalidArgumentException.
akita_media_register_profile('square', ['widths' => [320, 640], 'aspect_ratio' => '1:1']);akita_media_profiles(): array- Alle Profile nach Namen indiziert.
$hero = akita_media_profiles()['hero'] ?? null;akita_media_resolve_acf(mixed $value, array $args = []): array- Sammlung mit multiple, attachment_ids, items und error.
$gallery = akita_media_resolve_acf(get_field('gallery'), ['profile' => 'gallery']);akita_media_enable_content_integration(array $config = []): bool- Aktiviert Bild-/Blockfilter für diese Anfrage und liefert true.
akita_media_enable_content_integration(['picture_class' => 'site-picture']);akita_media_disable_content_integration(): void- Entfernt automatische Filter für diese Anfrage.
akita_media_disable_content_integration();akita_media_background_style(int $attachmentId, array $args = []): string- CSS mit Original und optionalem image-set(); leer ohne Fallback.
$css = akita_media_background_style($imageId, ['profile' => 'hero']);akita_media_background_url(int $attachmentId, array $args = []): string- Format-URL oder Original. Standardformat: original.
$url = akita_media_background_url($imageId, ['format' => 'webp']);akita_media_source_id(int $attachmentId): int- Physische Quell-ID mit WPML-Zuordnung.
$sourceId = akita_media_source_id($attachmentId);akita_media_video_id(int $attachmentId): int- ID des erzeugten MP4, sonst Original-Anhang-ID.
$deliveryId = akita_media_video_id($videoId);akita_media_video_url(int $attachmentId): string- URL von MP4 oder Original, gegebenenfalls leer.
$url = akita_media_video_url($videoId);akita_media_video_webm_id(int $attachmentId): int- WebM-Anhang-ID oder 0.
$webmId = akita_media_video_webm_id($videoId);akita_media_video_webm_url(int $attachmentId): string- WebM-URL oder leerer String.
$webmUrl = akita_media_video_webm_url($videoId);akita_media_video_sources(int $attachmentId): array- Liste {url, type}, WebM vor dem Fallback. Ohne URLs leer.
$sources = akita_media_video_sources($videoId);akita_media_video(int $attachmentId, array $args = []): string- video/source-HTML mit optionalem Poster oder leerer String.
echo akita_media_video($videoId, ['controls' => true]);akita_media_poster_id(int $attachmentId): int- Poster-Anhang-ID oder 0.
$posterId = akita_media_poster_id($videoId);akita_media_poster_url(int $attachmentId, string $size = 'full'): string- Poster-URL in einer WordPress-Größe oder leerer String.
$posterUrl = akita_media_poster_url($videoId, 'large');akita_media_status(int $attachmentId): string- Gespeicherter Bild-/Videostatus der Quelle oder leerer String.
$status = akita_media_status($attachmentId);akita_media_is_ready(int $attachmentId): bool- Nur beim Quellstatus ready wahr.
$ready = akita_media_is_ready($attachmentId);
11
Prüfung und Fehlerbehebung
Dateierzeugung und tatsächliche Ausgabe auf einer realen Seite prüfen.
- Website-Zustand auf Encoder und Zeitpläne prüfen. Standard-WP-Cron erfordert kein SSH.
- Fehlende Bildgrößen regenerieren, Bibliothek scannen und Verarbeitung abwarten.
- picture/source und Netzwerk-Anfragen prüfen. Ein fehlendes Format verwendet erwartungsgemäß den Fallback.
- Nach Richtlinienänderungen neu verarbeiten und betroffene Seiten-/CDN-Caches leeren.
- Galerien lazy laden; eager/high für das tatsächliche Hauptbild reservieren. sizes beschreibt das Layout, keine neue Dateigröße.