What is the best way to store thumbnails for a non-image attachment?

I’m in the process of extending Document Gallery to support generating and displaying thumbnails for each document.

I’ve got thumbnail generation under control, but I’m curious as to what best practices for storing both the physical image files for each thumb and the references to these file paths. The research I’ve done thus far turned up attachment thumbnails (which appear to be different than Post Thumbnails) and these seem like an ideal foundation for creating this new functionality without reinventing the wheel. Documentation on how to hook into this existing framework, specifically, creating new attachment thumbnails, seems to be quite sparse. As much as possible, I’d like to use existing functionality rather than duplicating functionality.

Read More

With this in mind, looking at wp_get_attachment_thumb_file (below), it seems like I could simply set the thumb filed in attachment metadata to the name of the thumbnail which would live in the same directory as the attachment file. In doing this, I would also indirectly handle removing the thumbnail file when the attachment is deleted since wp_delete_attachment checks for a thumbnail on deletion and removes it if present.

I believe this solution will work, but I was hoping someone with a working knowledge of all of this could verify not only that it will work but, more importantly, whether it is the “best” way to handle my needs.

function wp_get_attachment_thumb_file( $post_id = 0 ) {
    $post_id = (int) $post_id;
    if ( !$post = get_post( $post_id ) )
        return false;
    if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
        return false;

    $file = get_attached_file( $post->ID );

    if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) )
        return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
    return false;
}

Related posts