This documentation describes unreleased changes on the main branch.

MediaLibrary

Class.

use Saij\WpAppFramework\Asset\MediaLibrary;

Imports static assets of the instance into the media library, for content that needs attachments: an #[Image] field takes the ID of an attachment, not a file of the theme. Call it where content is created, for example in a method with #[OnActivation] or in a WP-CLI command.

The import is idempotent per site: an asset already imported with the same content returns its attachment. A changed file becomes a new attachment; the old one stays, because content may use it. Attachments stay in the media library when the theme or plugin is removed.

Methods

import()

import(string $name): int

The attachment of an asset of the instance, imported on the first call.

ParameterTypeDefaultDescription
namestringrequiredThe asset, e.g. images/hero.jpg; files of overrides and of the child theme cannot be imported.

Returns int: The ID of the attachment.

Throws InvalidArgumentException: If the name names no asset of the instance.

Throws RuntimeException: If WordPress cannot store the file or create the attachment.