Halloween Sale 15% off Ends in See the plans
PHP

Media Library Overview Widget

Adds a dashboard widget breaking the Media Library down into images, videos, audio and documents, with uploads this month and the number of files not attached to any content.

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

Code

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

    wp_add_dashboard_widget( 'wpide_media_overview', 'Media Library overview', function () {
        global $wpdb;

        $stats = get_transient( 'wpide_media_overview' );
        if ( false === $stats ) {
            $types = $wpdb->get_results(
                "SELECT SUBSTRING_INDEX(post_mime_type, '/', 1) AS kind, COUNT(*) AS total FROM {$wpdb->posts} WHERE post_type = 'attachment' GROUP BY kind",
                OBJECT_K
            );
            $stats = array(
                'image'      => (int) ( $types['image']->total ?? 0 ),
                'video'      => (int) ( $types['video']->total ?? 0 ),
                'audio'      => (int) ( $types['audio']->total ?? 0 ),
                'other'      => (int) array_sum( array_map( function ( $row ) {
                    return in_array( $row->kind, array( 'image', 'video', 'audio' ), true ) ? 0 : (int) $row->total;
                }, $types ) ),
                'month'      => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_date >= %s", wp_date( 'Y-m-01 00:00:00' ) ) ),
                'unattached' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_parent = 0" ),
            );
            set_transient( 'wpide_media_overview', $stats, HOUR_IN_SECONDS );
        }

        $tiles = array(
            'Images'    => array( $stats['image'], 'upload.php?post_mime_type=image' ),
            'Videos'    => array( $stats['video'], 'upload.php?post_mime_type=video' ),
            'Audio'     => array( $stats['audio'], 'upload.php?post_mime_type=audio' ),
            'Documents' => array( $stats['other'], 'upload.php?mode=list' ),
        );

        echo '<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(90px,1fr));gap:8px;margin-bottom:12px">';
        foreach ( $tiles as $label => $tile ) {
            printf(
                '<a href="%s" style="display:block;border:1px solid #dcdcde;border-radius:4px;padding:10px;text-align:center;text-decoration:none"><span style="display:block;font-size:22px;font-weight:600;color:#1d2327">%s</span>%s</a>',
                esc_url( admin_url( $tile[1] ) ),
                esc_html( number_format_i18n( $tile[0] ) ),
                esc_html( $label )
            );
        }
        echo '</div>';
        printf(
            '<p>%s uploaded this month · <a href="%s">%s not attached to any content</a></p>',
            esc_html( number_format_i18n( $stats['month'] ) ),
            esc_url( admin_url( 'upload.php?mode=list&attachment-filter=detached' ) ),
            esc_html( number_format_i18n( $stats['unattached'] ) )
        );
    } );
} );

add_action( 'add_attachment', function () {
    delete_transient( 'wpide_media_overview' );
} );

Notes

Counts are cached for an hour; the "unattached" link opens the matching Media Library view.

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.