"Missing Alt Text" Filter in the Media Library
Adds a dropdown to the Media Library list view that shows only images without alternative text, so you can fix them one by one.
Opens WPIDE on
Code
add_action( 'restrict_manage_posts', function ( $post_type ) {
if ( 'attachment' !== $post_type ) {
return;
}
$current = isset( $_GET['wpide_alt'] ) ? sanitize_key( wp_unslash( $_GET['wpide_alt'] ) ) : '';
echo '<label class="screen-reader-text" for="wpide-alt-filter">Filter by alt text</label>';
echo '<select name="wpide_alt" id="wpide-alt-filter">';
echo '<option value="">All alt text states</option>';
echo '<option value="missing"' . selected( $current, 'missing', false ) . '>Missing alt text</option>';
echo '</select>';
} );
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() || ! $query->is_main_query() || empty( $_GET['wpide_alt'] ) || 'missing' !== $_GET['wpide_alt'] ) {
return;
}
if ( 'attachment' !== $query->get( 'post_type' ) ) {
return;
}
$query->set( 'post_mime_type', 'image' );
$query->set( 'meta_query', array(
'relation' => 'OR',
array( 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS' ),
array( 'key' => '_wp_attachment_image_alt', 'value' => '' ),
) );
} );
Notes
Works in the list view of the Media Library (upload.php?mode=list).
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.