Media PHP Beginner

Show Images Missing Alt Text in the Media Library

Add an "Alt text" column to the Media Library list that flags images with no alt text, and a filter to show only those.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 32 lines
add_filter( 'manage_media_columns', function ( array $columns ): array {
	$columns['nahnu_alt'] = 'Alt text';
	return $columns;
} );

add_action( 'manage_media_custom_column', function ( string $column, int $post_id ): void {
	if ( 'nahnu_alt' !== $column || ! wp_attachment_is_image( $post_id ) ) {
		return;
	}
	$alt = trim( (string) get_post_meta( $post_id, '_wp_attachment_image_alt', true ) );
	echo '' === $alt ? '<span style="color:#b32d2e">Missing</span>' : esc_html( wp_trim_words( $alt, 8 ) );
}, 10, 2 );

// Add ?nahnu_missing_alt=1 to the Media list URL to show only images without alt text.
add_action( 'pre_get_posts', function ( WP_Query $query ): void {
	global $pagenow;
	if ( ! is_admin() || 'upload.php' !== $pagenow || ! $query->is_main_query() || empty( $_GET['nahnu_missing_alt'] ) ) {
		return;
	}
	$query->set( 'post_mime_type', 'image' );
	$query->set( 'meta_query', [
		'relation' => 'OR',
		[ 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS' ],
		[ 'key' => '_wp_attachment_image_alt', 'value' => '' ],
	] );
} );

add_action( 'restrict_manage_posts', function ( string $post_type ): void {
	if ( 'attachment' === $post_type ) {
		printf( '<a class="button" href="%s">Missing alt text</a> ', esc_url( admin_url( 'upload.php?mode=list&nahnu_missing_alt=1' ) ) );
	}
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Alt text describes images for people using screen readers, and helps search engines too. It’s easy to forget, and the Media Library gives no way to find the gaps. This snippet adds a column and a one-click filter.

How it works

manage_media_columns adds the column, which shows the start of each image’s alt text, or “Missing” in red.

The “Missing alt text” button adds nahnu_missing_alt=1 to the URL. pre_get_posts then limits the list to images whose _wp_attachment_image_alt meta is empty or doesn’t exist.

Things to watch

  • Alt text set on an Image block in a post is stored in that post, not the Media Library. An image can show “Missing” here but still have alt text where it’s used.
  • Decorative images can have empty alt text on purpose.

How to test it

Go to Media → Library in list view and click “Missing alt text”. Only images without alt text should be listed.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only