Add file name column to media library

I’ve migrated hosts for a WordPress site, and a lot of my images have the same title.

I am trying to pinpoint one media file by the file name, but file name is not a column listed in Media Library.

Read More

I don’t want to have to wade through dozens of images with the same title trying to find the problem file name, so I can find the correct URL to this image.

How can I add file name to the list of columns in Media Library?

I can’t search the media library by file name either.

Related posts

Leave a Reply

3 comments

  1. Here you go, this code not only lists all filenames in Library but also allows you to sort them by name:

    // Add the column
    function filename_column( $cols ) {
        $cols["filename"] = "Filename";
        return $cols;
    }
    
    // Display filenames
    function filename_value( $column_name, $id ) {
        $meta = wp_get_attachment_metadata( $id );
        echo substr( strrchr( $meta['file'], '/' ), 1); 
        // Used a few PHP functions cause 'file' stores local url to file not filename
    }
    
    // Register the column as sortable & sort by name
    function filename_column_sortable( $cols ) {
        $cols["filename"] = "name";
        return $cols;
    }
    
    // Hook actions to admin_init
    function hook_new_media_columns() {
        add_filter( 'manage_media_columns', 'filename_column' );
        add_action( 'manage_media_custom_column', 'filename_value', 10, 2 );
        add_filter( 'manage_upload_sortable_columns', 'filename_column_sortable' );
    }
    add_action( 'admin_init', 'hook_new_media_columns' );
    
  2. The code above for sorting with Filename works, if you change 2 things.

    // Add the column
    function filename_column( $cols ) {
        $cols["filename"] = "Filename";
        return $cols;
    }
    
    // Display filenames
    function filename_value( $column_name, $id ) {
        $meta = wp_get_attachment_metadata( $id );
        echo basename( $meta['file'] );  // CHANGE 1
        // Used a few PHP functions cause 'file' stores local url to file not filename
    }
    
    // Register the column as sortable & sort by name
    function filename_column_sortable( $cols ) {
        $cols["filename"] = "Filename"; // CHANGE 2
        return $cols;
    }
    
    // Hook actions to admin_init
    function hook_new_media_columns() {
        add_filter( 'manage_media_columns', 'filename_column' );
        add_action( 'manage_media_custom_column', 'filename_value', 10, 2 );
        add_filter( 'manage_upload_sortable_columns', 'filename_column_sortable' );
    }
    add_action( 'admin_init', 'hook_new_media_columns' );
    
  3. I’m not sure if this answers the question directly but I was trying to get a “URL” column on the Media Library page (to avoid clicking the edit link every time…) and found this code snippet which does exactly that.

    I played with it a little to get it to sort the URLs alphabetically and to strip the domain, to improve my workflow a bit more. Check it out:

    <?php
    function muc_column($cols) {
        $cols["media_url"] = "URL";
        return $cols;
    }
    
    function muc_value($column_name, $id) {
        if ($column_name == "media_url")
    
        $subject = wp_get_attachment_url($id);
        $grab = array("http://example.com", "http://another.example.com");
        $replaceWith = '';
        $path = str_replace($grab, $replaceWith, $subject);
    
        echo '<input
                    type="text"
                    width="100%"
                    onclick="jQuery(this).select();"
                    value="'.$path.'" />';
    }
    
    function muc_column_sortable($cols) {
        $cols["media_url"] = "name";
        return $cols;
    }
    
    add_filter('manage_media_columns', 'muc_column');
    add_action('manage_media_custom_column', 'muc_value', 10, 2);
    add_filter('manage_upload_sortable_columns', 'muc_column_sortable'); 
    ?>
    

    I should mention it also presents the URL in an <input> that automatically selects the text when clicked, a nice feature for a quick copy and paste.