0

I'm trying to display the description of each file on the media library browser screen. I've found some code that adds a filename column, but I'm trying to modify it so that it will display the description field instead of the filename. I'm not quite sure of how to do this. The description column displays, but doesn't actually show the description data for each file. I think I'm getting hung up on this line:

echo substr(strrchr($meta['attachment_content'], '/' ), 1);

but it's possible that there are other issues.

Below is the full code:

function description_column($cols) {
    $cols["description"] = "Description";
    return $cols;
}

function description_value($column_name, $id) {
    $meta = wp_get_attachment_metadata($id);
    echo substr(strrchr($meta['attachment_content'], '/' ), 1);
}

function description_column_sortable($cols) {
    $cols["description"] = "name";
    return $cols;
} 

function hook_new_media_columns() {
    add_filter('manage_media_columns', 'description_column');
    add_action('manage_media_custom_column', 'description_value', 10, 2);
    add_filter('manage_upload_sortable_columns', 'description_column_sortable');
}

add_action('admin_init', 'hook_new_media_columns');

Thanks for your help!

1 Answer 1

0

I think I'm getting hung up on this line:

echo substr(strrchr($meta['attachment_content'], '/' ), 1);

Yep! Try this version of the description_value function:

function description_value($column_name, $id) {
    echo  get_the_content($id);
}

Media items are posts of the type attachment. The description is stored in the post_content property of the post object. You can see all of the fields by doing a print_r( get_post( $id ) ); just in case you wanted to make some other columns. By the way, wp_get_attachment_metadata will give you access to the EXIF data of the image, which I don't think you want here.

2
  • Thank you! And thanks for the extra info, that will definitely come in handy. Nov 8, 2013 at 4:09
  • No prob, glad I could help! Nov 8, 2013 at 4:19

Your Answer

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

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