How to Display EXIF Metadata in Media Library Admin Column in WordPress

Home » Snippets » How to Display EXIF Metadata in Media Library Admin Column in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re a photographer or blogger who frequently uploads images to your WordPress site, you may have noticed that the Media Library only displays basic information about your images, such as file name and size. However, if you’re interested in displaying more detailed information about your images, such as EXIF metadata, you’re in luck. In this article, we’ll show you how to add an additional column to the Media Library admin page that displays EXIF metadata for each image, making it easier for you to manage and organize your media library.

					function wpturbo_add_exif_column( $columns ) {
    $columns['exif'] = __( 'EXIF Data', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_upload_columns', 'wpturbo_add_exif_column' );

function wpturbo_show_exif_content( $column_name, $post_id ) {
    if ( $column_name === 'exif' ) {
        $image_meta      = wp_get_attachment_metadata( $post_id );
        $image_exif_data = $image_meta['image_meta'];
        if ( ! empty( $image_exif_data ) ) {
            $image_exif_string = '';
            foreach ( $image_exif_data as $key => $value ) {
                if ( is_array( $value ) ) {
                    $value = implode( ', ', $value );
                }
                $image_exif_string .= ucfirst( str_replace( '_', ' ', $key ) ) . ': ' . $value . '<br/>';
            }
            echo $image_exif_string;
        } else {
            echo __('No EXIF data found', 'wpturbo');
        }
    }
}
add_action( 'manage_media_custom_column', 'wpturbo_show_exif_content', 10, 2 );
				

This code snippet allows you to display the EXIF data of an image in the WordPress media library admin column. This is useful especially if you work with photographs or images that need specific metadata information.

The first part of the code registers a new column to the media library admin list view by adding an ‘exif’ column to the list of columns. This is done by adding a new element to the $columns array:

function wpturbo_add_exif_column( $columns ) {
    $columns['exif'] = __( 'EXIF Data', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_upload_columns', 'wpturbo_add_exif_column' );

Then, we define a new function called wpturbo_show_exif_content, which is responsible for showing the content in the newly created admin column. This function first checks whether the column name is ‘exif’ by comparing the $column_name parameter. If it is, it retrieves the image metadata and extracts the EXIF data. The EXIF data is organized in an array called $image_meta['image_meta'].

function wpturbo_show_exif_content( $column_name, $post_id ) {
    if ( $column_name === 'exif' ) {
        $image_meta = wp_get_attachment_metadata( $post_id );
        $image_exif_data = $image_meta['image_meta'];

The next step is to loop through each element in the $image_exif_data array and format the data as a string that can be displayed. If the value is an array, it is converted to a string separated by commas. The keys in the array are capitalized and the underscore is replaced by a space. Finally, it is displayed in the admin column by echoing it to the page.

if ( ! empty( $image_exif_data ) ) {
    $image_exif_string = '';
    foreach ( $image_exif_data as $key => $value ) {
        if ( is_array( $value ) ) {
            $value = implode( ', ', $value );
        }
        $image_exif_string .= ucfirst( str_replace( '_', ' ', $key ) ) . ': ' . $value . '<br/>';
    }
    echo $image_exif_string;
} else {
    echo __('No EXIF data found', 'wpturbo');
}

This function is then added as an action to the manage_media_custom_column hook, which ensures that it is executed when WordPress renders the media library admin column.

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

If there is no EXIF data for the image, the function will display ‘No EXIF data found’ in the column instead.

Register an account to save your snippets or go Pro to get more features.