3

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.

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.

1
  • You can also try my plugin, Media Library Assistant. It has a wide variety of features that help you find images, including the ability to add file name as a sortable column. wordpress.org/extend/plugins/media-library-assistant
    – user27545
    Commented Feb 15, 2013 at 21:00

3 Answers 3

8

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' );
3
  • what file do I add this code to?
    – Steve
    Commented Feb 2, 2013 at 10:43
  • 1
    functions.php of course! :)
    – Bart Karp
    Commented Feb 3, 2013 at 0:05
  • 1
    Instead of substr( strrchr( $meta['file'], '/' ), 1) we can use basename( $meta['file'] ). The result is the same, but the code becomes more readable, php.net/manual/en/function.basename.php Commented Jul 5, 2020 at 20:06
1

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' );
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.