Add column for attachment file size

EDIT. I got this working, see my own answer below:

Now, if someone has any clue as to how I can get this sortable, please chime in! :)

---------------- Original question ----------------

How can I add a custom column to the attachment post type? I'm finding ways to add them to posts, pages or custom post types. But for native post types like attachments, how?

I would like to display the attachment file size in it's own column.

Topic screen-columns attachments Wordpress

Category Web


Working code:

// Add custom column with file size info to attachment page
add_filter( 'manage_media_columns', 'bb2_manage_media_columns', 10, 2 );
function bb2_manage_media_columns( $columns )
{
    $columns['filesize'] = __( 'Storlek', 'bb2' );
    return $columns;
}

add_action( 'manage_media_custom_column', 'bb2_manage_media_custom_column', 10, 2 );
function bb2_manage_media_custom_column( $column_name, $id )
{
    switch ( $column_name )
    {
        case 'filesize' :
            $bytes = filesize(get_attached_file($id));
            echo size_format($bytes);
        break;

        default :
        break;
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.