0

I use the code below to show the alt tag in the Media Library column, but I also use an extra column/code to show the image dimensions. The only problem now, is that the dimensions are also shown in the alt tag column. What is it in the code that adds the dimension data to the alt text?

Hope someone can help me out, thanks!

function wpse_media_extra_column( $cols ) {
    $cols["alt"] = "ALT";
    return $cols;
}
function wpse_media_extra_column_value( $column_name, $id ) {
    if( $column_name == 'alt' )
        echo get_post_meta( $id, '_wp_attachment_image_alt', true);
}
add_filter( 'manage_media_columns', 'wpse_media_extra_column' );
add_action( 'manage_media_custom_column', 'wpse_media_extra_column_value', 10, 2 );

function wh_column( $cols ) {
    $cols["dimensions"] = "Dimensions (w, h)";
    return $cols;
}
function wh_value( $column_name, $id ) {
    $meta = wp_get_attachment_metadata($id);
           if(isset($meta['width']))
           echo $meta['width'].' x '.$meta['height'];
}
add_filter( 'manage_media_columns', 'wh_column' );
add_action( 'manage_media_custom_column', 'wh_value', 10, 2 );

enter image description here

1
  • Your wh_value function isn't checking the column name. You're doing it correctly in wpse_media_extra_column_value so just do that. Dec 9, 2021 at 10:38

1 Answer 1

1

You don't have to create a new function each time you add a new admin column. You can use below functions to show both your media data in separate admin columns.

function column_id($columns) {
    $columns['media_alt'] = __('Alt');
    $columns['media_dimensions'] = __('Dimensions');
    return $columns;
} 
add_filter( 'manage_media_columns', 'column_id' );
 
function column_id_row($columnName, $columnID){
    if ( $columnName == 'media_alt') {
        $image_alt = get_post_meta($columnID, '_wp_attachment_image_alt', true);
        echo $image_alt;
    }
    if ( $columnName == 'media_dimensions') {
        $image_size = wp_get_attachment_image_src($columnID, 'full');
        if ( $image_size ) {
            echo $image_size[1] . ' x ' . $image_size[2];
        }
    }
}
add_filter( 'manage_media_custom_column', 'column_id_row', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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