Halloween Sale 15% off Ends in See the plans
PHP

"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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.