lugoazul
9/24/2017 - 11:33 AM

WP-Media Library File Dimension Column

function jajadi_show_dimensions_size_column_register($columns) {
	/* translators: Column name */
    $columns['dimensions'] = __('Dimensions', 'show-dimensions-in-library');

    return $columns;
}


function jajadi_show_dimensions_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');
	$metadata = wp_get_attachment_metadata( $post_id );
	$width = $metadata['width'];
	$height = $metadata['height'];

    echo esc_html("{$width}×{$height}");
}


function jajadi_show_dimensions_load_textdomain() {
	load_plugin_textdomain( 'show-dimensions-in-library', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}

// Register the column as sortable
function jajadi_show_dimensions_size_column_register_sortable( $columns ) {
	$columns['dimensions'] = 'dimensions';
 
	return $columns;
}


function jajadi_show_dimensions_size_column_orderby( $query ) {
    if( ! is_admin() )
        return;
 
    $orderby = $query->get( 'orderby');
 
    if( 'dimensions' == $orderby ) {
        $query->set('meta_key','_wp_attachment_metadata');
        $query->set('orderby','meta_value');
    }
}


// Hooks a function on to a specific action.
add_action( 'plugins_loaded', 'jajadi_show_dimensions_load_textdomain');
add_filter('manage_upload_columns', 'jajadi_show_dimensions_size_column_register');
add_action('manage_media_custom_column', 'jajadi_show_dimensions_size_column_display', 10, 2);

add_filter( 'manage_upload_sortable_columns', 'jajadi_show_dimensions_size_column_register_sortable' );
add_action( 'pre_get_posts', 'jajadi_show_dimensions_size_column_orderby' );