How To: Display Attachment/Media Information in WordPress Media Library

Advertisement

Displaying media information right in front of you could be a time-saving in Search Engine Optimization.

Below snippet would add a new column with File size, Caption and a media Description, cool!

Let’s get started

Register Our Column

In WordPress, there are countless hooks we can use, this time we’ll be using a hook to register our column to the Media table.

Code below added a new column Media Info to the Media library table.


add_filter( 'manage_media_columns', 'rs_themefi_set_custom_edit_media_columns');

function rs_themefi_set_custom_edit_media_columns($columns) {
    unset( $columns['author'] );
    $columns['media_info'] 		= __( 'Media Info', 'swoop-file-info' );

    return $columns;
}

Adding Column Values

After we registered our column we can then create a new hook and fill the column with corresponding values.


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

function rs_themefi_custom_media_column( $column, $post_id ) {
    switch ( $column ) {

        case 'media_info' :
            $file_size 				= 0;

            $attachment_file 		= get_post_meta( $post_id, '_wp_attached_file', true );
            $ttachment_image_alt 	= get_post_meta( $post_id, '_wp_attachment_image_alt', true );
            $file_path				= get_attached_file( $post_id );
            
            if( file_exists( $file_path ) )
                $file_size = filesize( $file_path );
            
            // echo and display formatted number
            $file_size		= number_format($file_size / 1000, 2 ) . ' KB';
            
            if ( ! $post = get_post( $post_id ) ) {
                return false;
            }
            
            if ( 'attachment' !== $post->post_type ) {
                return false;
            }
            
            $caption 		= $post->post_excerpt;
            $description 	= $post->post_content;
            ?>
            <p><strong>Size:</strong> <?php echo $file_size; ?></p>	
            <p><?php if( $caption ) : ?><strong>Caption:</strong> <?php echo $caption; ?><?php endif; ?></p>	
            <p><?php if( $description ) : ?><strong>Description:</strong> <?php echo $description; ?><?php endif; ?></p>
            <?php
        break;

    }
}

Complete Code

Here’s a complete PHP code, this part you can copy and paste and have a go.


add_filter( 'manage_media_columns', 'rs_themefi_set_custom_edit_media_columns');
add_action( 'manage_media_custom_column', 'rs_themefi_custom_media_column', 10, 2 );

function rs_themefi_set_custom_edit_media_columns($columns) {
    unset( $columns['author'] );
    $columns['media_info'] 		= __( 'Media Info', 'swoop-file-info' );

    return $columns;
}

function rs_themefi_custom_media_column( $column, $post_id ) {
    switch ( $column ) {

        case 'media_info' :
            $file_size 				= 0;

            $attachment_file 		= get_post_meta( $post_id, '_wp_attached_file', true );
            $ttachment_image_alt 	= get_post_meta( $post_id, '_wp_attachment_image_alt', true );
            $file_path				= get_attached_file( $post_id );
            
            if( file_exists( $file_path ) )
                $file_size = filesize( $file_path );
            
            // echo and display formatted number
            $file_size		= number_format($file_size / 1000, 2 ) . ' KB';
            
            if ( ! $post = get_post( $post_id ) ) {
                return false;
            }
            
            if ( 'attachment' !== $post->post_type ) {
                return false;
            }
            
            $caption 		= $post->post_excerpt;
            $description 	= $post->post_content;
            ?>
            <p><strong>Size:</strong> <?php echo $file_size; ?></p>	
            <p><?php if( $caption ) : ?><strong>Caption:</strong> <?php echo $caption; ?><?php endif; ?></p>	
            <p><?php if( $description ) : ?><strong>Description:</strong> <?php echo $description; ?><?php endif; ?></p>
            <?php
        break;

    }
}

That’s it, happy coding ^_^

Advertisement