Classic theme media images

From Lonsdale201/wp-agent-skills · View interactive version

Classic theme media images is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Build or audit media and image output in classic WordPress themes on WP 7.1. Covers `add_theme_support( 'post-thumbnails' )`, `add_image_size()`, `set_post_thumbnail_size()`, `has_post_thumbnail()`, `the_post_thumbnail()`, `get_the_post_thumbnail()`, `wp_get_attachment_image()`, responsive `srcset`

How to use this skill

Skill file

Classic Theme Media and Images

Use this when adding or reviewing featured images, attachment images, custom image sizes, responsive image attributes, image accessibility, or theme asset images in a classic PHP theme.

When to Use This Skill

Enable Featured Images

Enable post thumbnails during theme setup.

add_action( 'after_setup_theme', 'mytheme_setup' );

function mytheme_setup() {
	add_theme_support( 'post-thumbnails' );
}

Rules:

Register Image Sizes

Register reusable sizes in theme setup.

add_action( 'after_setup_theme', 'mytheme_image_sizes' );

function mytheme_image_sizes() {
	set_post_thumbnail_size( 1200, 675, true );
	add_image_size( 'mytheme-hero', 1600, 900, true );
	add_image_size( 'mytheme-card', 640, 360, true );
	add_image_size( 'mytheme-portrait', 480, 640, array( 'center', 'top' ) );
}

Rules:

Render Featured Images

Use core thumbnail functions.

if ( has_post_thumbnail() ) {
	the_post_thumbnail(
		'mytheme-card',
		array(
			'class' => 'entry-card-image',
		)
	);
}

Rules:

Attachment Images

For attachment IDs, use wp_get_attachment_image().

echo wp_get_attachment_image(
	$attachment_id,
	'mytheme-card',
	false,
	array(
		'class' => 'media-card-image',
	)
);

Core adds useful defaults:

Do not replace this with a manual <img src="..."> unless the image is not a WordPress attachment.

WordPress 7.1 metadata contract

wp_get_attachment_metadata() now returns false if stored or filtered

metadata is not an array. When a sizes key exists but a filter makes it

non-array, core normalizes it to an empty array. Treat the return as

array|false, and treat sizes as optional; never index it blindly.

wp_filesize() now always returns a non-negative integer. Numeric filter

results are cast, negative pre-filter values are ignored, and a negative or

non-numeric final filter value is clamped to zero. Zero still means either an

empty file or an unavailable size, so it is not proof that the attachment is

invalid.

WordPress 7.1 can process supported images in the browser before upload. Theme

rendering must remain agnostic to whether the server or client created the

sub-sizes: use attachment IDs and core image helpers, not filename conventions

or assumptions that every size was derived in a single server request.

Alt Text

Rules:

For a linked post thumbnail in an archive card, the link text/title may already describe the post. In that case, an empty alt can avoid repetition:

printf(
	'<a class="entry-card-link" href="%1$s">%2$s</a>',
	esc_url( get_permalink() ),
	get_the_post_thumbnail(
		get_the_ID(),
		'mytheme-card',
		array( 'alt' => '' )
	)
);

Responsive and Performance Attributes

WordPress image functions can output srcset, sizes, loading, decoding, and fetchpriority.

Rules:

Example hero image:

the_post_thumbnail(
	'mytheme-hero',
	array(
		'class'         => 'hero-image',
		'loading'       => 'eager',
		'fetchpriority' => 'high',
	)
);

Use this only for the page's primary visual, not every singular featured image by default.

Theme Asset Images

For static images shipped with the theme, use theme file helpers and normal escaping.

<img
	src="<?php echo esc_url( get_theme_file_uri( 'assets/images/pattern.png' ) ); ?>"
	alt=""
	width="320"
	height="180"
>

Rules:

Background Images

When an image carries content, do not put it only in CSS background.

Rules:

Review Checklist

Common Mistakes

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.