3

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.

  • Please show us what have you tried so far. – Nilambar Sharma Jul 28 '15 at 8:44
  • I got it working and updated the original question. If anyone knows how to get it sortable aswell, that would be a great contribution. – Johan Dahl Jul 28 '15 at 8:52
  • Please post your solution as an answer, which is how it works on WPSE - thanks! – Nicolai Jul 28 '15 at 8:59
  • Thanks for telling me, I just did - can't accept it until 2 days though! – Johan Dahl Jul 28 '15 at 9:13
  • Yea, one has to wait, but on the other hand you can even earn a badge for answering your own question :) If you want me or others to get the message add @username. I think - not 100% certain, but almost - you have to take a look at manage_uploads_sortable_columns for the sorting functionality. – Nicolai Jul 28 '15 at 9:25
2

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.