4

i'm trying to add image size in media library screen so will be simply to handle images. Anyone does it before me? Any help appreciated. Francesco

  • Codex: add_image_size(); – kaiser Oct 12 '11 at 19:59
  • Investigating this Q&A, I ended up assembling a two sortable columns solution (width & height). I posted it in this other question because there the sortable functionality is being asked... But @Rarst answer and link got me started in the first place. – brasofilo Jun 5 '12 at 14:41
9

Code based on Custom Sortable Columns

add_filter('manage_upload_columns', 'size_column_register');

function size_column_register($columns) {

    $columns['dimensions'] = 'Dimensions';

    return $columns;
}

add_action('manage_media_custom_column', 'size_column_display', 10, 2);

function size_column_display($column_name, $post_id) {

    if( 'dimensions' != $column_name || !wp_attachment_is_image($post_id)) return;

    list($url, $width, $height) = wp_get_attachment_image_src($post_id, 'full');

    echo esc_html("{$width}×{$height}");
}
  • Its not sortable as stated, just lists the sizes only – iamkingsleyf Jul 27 at 7:19
  • @iamkingsleyf the question didn't ask for sortable, I merely used linked post as starting point at the time. :) – Rarst Jul 30 at 15:58
3

Thanks, I was looking that for a while.

And for sizes (meaning weight)

add_filter( 'manage_media_columns', 'sk_media_columns_filesize' );
/**
 * Filter the Media list table columns to add a File Size column.
 *
 * @param array $posts_columns Existing array of columns displayed in the Media list table.
 * @return array Amended array of columns to be displayed in the Media list table.
 */
function sk_media_columns_filesize( $posts_columns ) {
    $posts_columns['filesize'] = __( 'File Size', 'my-theme-text-domain' );

    return $posts_columns;
}

add_action( 'manage_media_custom_column', 'sk_media_custom_column_filesize', 10, 2 );
/**
 * Display File Size custom column in the Media list table.
 *
 * @param string $column_name Name of the custom column.
 * @param int    $post_id Current Attachment ID.
 */
function sk_media_custom_column_filesize( $column_name, $post_id ) {
    if ( 'filesize' !== $column_name ) {
        return;
    }

    $bytes = filesize( get_attached_file( $post_id ) );

    echo size_format( $bytes, 2 );
}

add_action( 'admin_print_styles-upload.php', 'sk_filesize_column_filesize' );
/**
 * Adjust File Size column on Media Library page in WP admin
 */
function sk_filesize_column_filesize() {
    echo
    '<style>
        .fixed .column-filesize {
            width: 10%;
        }
    </style>';
}

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.