Created
November 26, 2018 18:26
-
-
Save hearvox/a8d51e7d193524dc056bfb5adc94af7e to your computer and use it in GitHub Desktop.
Add image data to new column to Media Library: file-size (bytes), image dimensions (pixels), and optional ratio between the two.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Add image data column to Media Library */ | |
/** | |
* Add columns (file-size) to the Media Library list table. | |
* | |
* @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 my_media_columns( $posts_columns ) { | |
$posts_columns['size'] = __( 'Size', 'headecon' ); | |
return $posts_columns; | |
} | |
add_filter( 'manage_media_columns', 'my_media_columns' ); | |
/** | |
* Add data (file-size and pixel dimensions) to items in the Media Library list table. | |
* | |
* Optionally add a radio between file-size and image area (pixels). | |
* | |
* @param string $column_name Name of the custom column. | |
* @param int $post_id Current Attachment ID. | |
*/ | |
function my_media_columns_data( $column_name, $post_id ) { | |
if ( 'size' !== $column_name ) { | |
return; | |
} | |
$img_data = $img_meta = $bytes = ''; // String vars. | |
$filesize = $width = $height = $ratio = 0; // Number vars. | |
if ( wp_attachment_is_image( $post_id ) ) { | |
$filesize = filesize( get_attached_file( $post_id ) ); | |
$bytes = size_format( $filesize, 1 ); | |
// Highlight sizes over 75 KB | |
if ( $filesize > 150000 ) { | |
$bytes = '<span class="size150kb">' . $bytes . '</span>'; | |
} elseif ( $filesize > 75000 ) { | |
$bytes = '<span class="size75kb">' . $bytes . '</span>'; | |
} | |
$img_meta = wp_get_attachment_metadata( $post_id ); | |
$img_data = "$bytes<br>{$img_meta['width']}x{$img_meta['height']}"; | |
// (Comment above $img_data and uncomment below to add:) | |
// Ratio of filesize to pixel area (lower means a better optimized image). | |
// $width = $img_meta['width']; | |
// $height = $img_meta['height']; | |
// $ratio = round( $filesize / ( $width * $height ), 1 ); // Radio of filesize to pixel area. | |
// $img_data = "{$bytes}<br>{$width}x{$height}<br>({$ratio})"; | |
} | |
echo $img_data; | |
} | |
add_action( 'manage_media_custom_column', 'my_media_columns_data', 10, 2 ); | |
/** | |
* Style columns in the in the Media Library list table. | |
* | |
* Prints <style> in <head> when 'admin-menu' stylesheet loads. | |
*/ | |
function my_media_columns_styles() { | |
$custom_css = " /* Media Library column styles */ | |
.fixed .column-size { width: 5rem; } | |
.size150kb { color: #ff0000; } | |
.size75kb { color: #ff5722; }"; | |
wp_add_inline_style( 'admin-menu', $custom_css ); | |
} | |
add_action( 'admin_print_styles-upload.php', 'my_media_columns_styles' ); | |
/** | |
* (Another way to:) Style columns in the in the Media Library list table. | |
* | |
*/ | |
/* | |
function headecon_media_columns_add_styles() { | |
echo | |
'<style> | |
.fixed .column-size { width: 5rem; } | |
.size150kb { color: #ff0000; } | |
.size75kb { color: #ff5722; } | |
</style>'; | |
} | |
// add_action( 'admin_print_styles-upload.php', 'headecon_media_columns_add_styles' ); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment