How to Add Attachment ID to Media Library Admin Columns in WordPress

Home » Snippets » How to Add Attachment ID to Media Library Admin Columns 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

Managing media files in WordPress can be a challenging task, especially when you have a large collection of files. By default, WordPress media library has several columns, such as file name, date uploaded, and file size. However, you may need additional columns, such as the attachment ID, to help you better organize your media files. In this article, we will show you how to add attachment ID as a column in your WordPress media library admin panel.

					function wpturbo_add_attachment_id_column( $columns ) {
    $columns['wpturbo_attachment_id'] = __( 'ID', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_media_columns', 'wpturbo_add_attachment_id_column' );

function wpturbo_attachment_id_column_content( $column_name, $attachment_id ) {
    if ( 'wpturbo_attachment_id' == $column_name ) {
        echo $attachment_id;
    }
}
add_action( 'manage_media_custom_column', 'wpturbo_attachment_id_column_content', 10, 2 );
				

In this tutorial, we will learn how to add a custom column to the Media Library admin page that displays the attachment ID for each media file.

The first part of the code defines a new function called wpturbo_add_attachment_id_column(). This function will add a new column to the media library table, with a column header text of "ID".

function wpturbo_add_attachment_id_column( $columns ) {
    $columns['wpturbo_attachment_id'] = __( 'ID', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_media_columns', 'wpturbo_add_attachment_id_column' );

We use the add_filter() function to add our new column definition to the manage_media_columns filter. We pass our wpturbo_add_attachment_id_column() function as a callback to this filter. This will ensure that our new column is added to the media library table.

The second part of the code defines another function called wpturbo_attachment_id_column_content(). This function will be responsible for displaying the attachment ID for each media file in our custom column. It takes two arguments: $column_name and $attachment_id.

function wpturbo_attachment_id_column_content( $column_name, $attachment_id ) {
    if ( 'wpturbo_attachment_id' == $column_name ) {
        echo $attachment_id;
    }
}
add_action( 'manage_media_custom_column', 'wpturbo_attachment_id_column_content', 10, 2 );

We use the add_action() function to hook our wpturbo_attachment_id_column_content() function into the manage_media_custom_column action. This action fires whenever WordPress renders the content for a custom column in the media library table.

Inside our function, we use an if statement to check if the $column_name parameter matches our custom column’s ID. If it does, we simply output the $attachment_id parameter, which is the ID of the current media file being rendered.

With these two functions added to our WordPress site, we can now see an additional "ID" column in our Media Library admin page, displaying the ID of each of our media files.

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