Adding ID Field To The Media Library Listing In WordPress Admin
If you manage a WordPress site with many media files, you might find it useful to see the attachment ID directly in the Media Library list. By default, WordPress does not show the media ID column, which can make referencing or debugging media items harder. This tutorial shows you how to media library show ID column WordPress with a quick code snippet you can add to your theme’s functions.php
file or a small custom plugin.
When to Use This
- You need to quickly find the attachment ID for shortcodes, custom queries, or debugging.
- You want to improve your workflow by seeing media IDs without opening each file.
- You are developing or managing a site with many media files and want better admin visibility.
Updated Code for Modern WordPress
WordPress has evolved, and the admin list tables now support hooks to add custom columns easily. The following code uses the manage_upload_columns
and manage_media_custom_column
filters to add an ID column to the Media Library list view.
Quick Fix: Add ID Column to Media Library
- Open your active theme’s
functions.php
file or create a small plugin. - Copy and paste the code below to add the ID column.
- Save and refresh the Media Library (list view) in the WordPress admin.
<?php
// Add ID column to Media Library list view
add_filter('manage_upload_columns', 'add_media_id_column');
function add_media_id_column($columns) {
$new_columns = array();
foreach ($columns as $key => $value) {
if ($key === 'title') { // Insert ID column before Title
$new_columns['media_id'] = 'ID';
}
$new_columns[$key] = $value;
}
return $new_columns;
}
add_action('manage_media_custom_column', 'show_media_id_column', 10, 2);
function show_media_id_column($column_name, $post_id) {
if ($column_name === 'media_id') {
echo $post_id;
}
}
?>
Step-by-Step: Add via functions.php or a Small Plugin
- Via functions.php:
- Access your site files via FTP or hosting file manager.
- Navigate to
wp-content/themes/your-active-theme/
. - Edit the
functions.php
file. - Paste the code snippet above at the end of the file.
- Save changes and upload if needed.
- Go to WordPress admin > Media > Library and switch to List View.
- You should see a new “ID” column showing the media item IDs.
- Via a small plugin:
- Create a new file named
media-library-id-column.php
. - Paste the following code inside:
- Upload the file to
wp-content/plugins/
. - Activate the plugin via WordPress admin > Plugins.
- Check the Media Library list view for the new ID column.
<?php /* Plugin Name: Media Library ID Column Description: Adds an ID column to the WordPress Media Library list view. Version: 1.0 Author: Your Name */ add_filter('manage_upload_columns', 'add_media_id_column'); function add_media_id_column($columns) { $new_columns = array(); foreach ($columns as $key => $value) { if ($key === 'title') { $new_columns['media_id'] = 'ID'; } $new_columns[$key] = $value; } return $new_columns; } add_action('manage_media_custom_column', 'show_media_id_column', 10, 2); function show_media_id_column($column_name, $post_id) { if ($column_name === 'media_id') { echo $post_id; } } ?>
- Create a new file named
Step-by-Step Test
- Log in to WordPress admin.
- Navigate to Media > Library.
- Make sure the view mode is set to List View (not Grid View).
- Look for the new ID column next to the Title column.
- Verify the numbers correspond to the media item IDs by clicking “Edit” on a media item and checking the URL (e.g.,
post.php?post=123&action=edit
).
Block Themes & Gutenberg Notes
Block themes and the Gutenberg editor do not affect the Media Library list table in the admin area. This code works independently of the front-end theme or editor. However, if you use a full site editing (FSE) block theme, the Media Library admin screen remains the same and will display the ID column as expected.
Common Pitfalls
- No ID column visible: Make sure you are in List View, not Grid View. The column only appears in List View.
- Code added but no effect: Clear any caching plugins or browser cache. Also, verify the code is in the active theme’s
functions.php
or the plugin is activated. - Syntax errors: Copy-paste carefully and ensure PHP tags are correct. A syntax error can break your site.
- Child theme usage: If you use a child theme, add the code to the child theme’s
functions.php
to avoid losing changes on updates.
Why This Happens
By default, WordPress does not include the attachment ID as a visible column in the Media Library list table. The list table is customizable via hooks, but the ID column is not enabled out of the box. Adding the ID column requires hooking into WordPress filters that control which columns are displayed and what content they show.
Works On
Environment | Notes |
---|---|
Apache, Nginx, LiteSpeed | Works on all common web servers as this is a WordPress PHP hook change. | WordPress Snippets Tags:Admin, Media Library, PHP, WordPress