Halloween Sale 15% off Ends in See the plans
PHP

Images Missing Alt Text Dashboard Widget

Adds a dashboard widget showing how many images in the Media Library have no alternative text, with a link to the most recent ones to fix.

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

Code

$wpide_alt_count = function () {
    global $wpdb;

    $count = get_transient( 'wpide_missing_alt_count' );

    if ( false === $count ) {
        $count = (int) $wpdb->get_var(
            "SELECT COUNT(p.ID) FROM {$wpdb->posts} p
             LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = '_wp_attachment_image_alt'
             WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE 'image/%' AND ( m.meta_value IS NULL OR m.meta_value = '' )"
        );
        set_transient( 'wpide_missing_alt_count', $count, 12 * HOUR_IN_SECONDS );
    }

    return (int) $count;
};

$wpide_alt_flush = function ( $meta_id, $object_id, $meta_key ) {
    if ( '_wp_attachment_image_alt' === $meta_key ) {
        delete_transient( 'wpide_missing_alt_count' );
    }
};
add_action( 'added_post_meta', $wpide_alt_flush, 10, 3 );
add_action( 'updated_post_meta', $wpide_alt_flush, 10, 3 );
add_action( 'add_attachment', function () {
    delete_transient( 'wpide_missing_alt_count' );
} );

add_action( 'wp_dashboard_setup', function () use ( $wpide_alt_count ) {
    if ( ! current_user_can( 'upload_files' ) ) {
        return;
    }

    wp_add_dashboard_widget( 'wpide_missing_alt', 'Images Missing Alt Text', function () use ( $wpide_alt_count ) {
        $count = $wpide_alt_count();

        if ( ! $count ) {
            echo '<p>Every image has alternative text. Nice work.</p>';
            return;
        }

        $recent = get_posts( array(
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'post_status'    => 'inherit',
            'posts_per_page' => 5,
            'no_found_rows'  => true,
            'meta_query'     => array(
                'relation' => 'OR',
                array( 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS' ),
                array( 'key' => '_wp_attachment_image_alt', 'value' => '' ),
            ),
        ) );

        printf( '<p><strong>%s</strong> image(s) have no alt text. Most recent:</p><ul>', esc_html( number_format_i18n( $count ) ) );
        foreach ( $recent as $image ) {
            printf( '<li><a href="%s">%s</a></li>', esc_url( get_edit_post_link( $image->ID ) ), esc_html( $image->post_title ? $image->post_title : '#' . $image->ID ) );
        }
        echo '</ul>';
    } );
} );

Notes

The count is cached for 12 hours and refreshed whenever an image's alt text changes.

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.