Skip to content

Instantly share code, notes, and snippets.

@mzdebo
Created March 13, 2021 10:00
Show Gist options
  • Save mzdebo/c3acbbc96be79f36de58dff492f20187 to your computer and use it in GitHub Desktop.
Save mzdebo/c3acbbc96be79f36de58dff492f20187 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////////////////////////
// //
// Add media size column to media library //
// //
////////////////////////////////////////////////////////////////////////////////////
// Add custom column to book list
function wh_column( $cols ) {
$cols["dimensions"] = "Dimensions (h)";
return $cols;
}
// Add content to above added custom column
function wh_value( $column_name, $id ) {
$meta = wp_get_attachment_metadata($id);
if(isset($meta['height']))
echo $meta['height'];
}
add_filter( 'manage_media_columns', 'wh_column' );
add_action( 'manage_media_custom_column', 'wh_value', 10, 2 );
// Register the column as sortable
add_filter( 'manage_upload_sortable_columns', 'my_sortable_dimensions_column' );
function my_sortable_dimensions_column( $columns ) {
$columns['dimensions'] = 'dimensions';
return $columns;
}
// Sort column
add_action( 'pre_get_posts', 'dimensions_column_orderby' );
function dimensions_column_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'height' == $orderby ) {
$query->set('meta_key','height');
$query->set('orderby','meta_value_num');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment