Make WP gallery display non-image files, independent of file type

UPDATE: Please see WP plugin to make gallery display PDFs, .docs, .xls, more!


Yesterday I made the WordPress gallery display all the post’s media according to the post_mime_type, e.g., pdf or msword. So if you want your post to have a gallery of images and then a gallery of pdf’s, or in my case msword’s and pdf’s, you can do that. However, I realized that it’s more useful to be able to create galleries according to some arbitrary common characteristic that the user designates (that is, the reason for dividing the gallery files). So, I came up with a different hack for the wp-includes/media.php file.

The first part is very similar to the previous solution, involving a revised definition of $attachments if the gallery shortcode parameter “linklist” is specified. Note that this parameter is merely one that I made up and could be called something else if it better suits your purpose. Under the default definition of $attachments, I added:

// ========================== anh hack 2010-05 =========================================
// get gallery to display list of links, eg for pdf's
if ( isset( $attr['linklist'] ) ) {
     $listfilegroup = $attr['linklist'];
     if ( !$attr['linklist'] )
     unset( $attr['linklist'] );

     $attachments = get_children( array('post_parent' => $id, 'post_status' =>
          'inherit', 'post_type' => 'attachment', 'order' => $order, 'orderby' =>
          $orderby) );
}
// ========================== end anh hack =============================================

Note that I didn’t use the value of linklist here at all; thus, the array $attachments contains all the media in the gallery (whereas the default fetched only images). Rather than limiting the results of my query for $attachments, I then added code (the next part) to limit the display of those results. Perhaps this is not the best way, since I imagine that limiting the query would be more efficient than limiting the display, but I was not able to limit the query according to post_content or post_excerpt, which correspond to editable fields for media (the “description” and “caption” respectively) in the default WP structure.

The next part of the hack replaces the command part of the foreach loop that generates the gallery output. Under foreach ( $attachments as $id => $attachment ) { , I commented out all of the commands (up to its closing bracket } ) and added:

// ========================== anh hack 2010-05 =========================================
/* the code in this hack block replaces the commented-out code. works with hack block
above; basically, if $listfilegroup is set, then list files according to $listfilegroup
(which is set for attachments only; $listfilegroup must match post_content - on admin
panel, this field is the media's "description" */

if (!empty($listfilegroup)) {

     if ($listfilegroup == trim($attachment->post_content))  {

          $link = isset($attr['link']) && 'file' == $attr['link'] ?
               wp_get_attachment_link($id, $size, false, false) :
               wp_get_attachment_link($id, $size, true, false);

          $output .= "<{$itemtag} class='gallery-item'>";
          $output .= "
          <{$icontag} class='gallery-icon'>
          $link
          </{$icontag}>";
          if ( $captiontag && trim($attachment->post_excerpt) ) {
               $output .= "
               <{$captiontag} class='gallery-caption'>
               " . wptexturize($attachment->post_excerpt) . "
               </{$captiontag}>";
          }
          $output .= "</{$itemtag}>";
          if ( $columns > 0 && ++$i % $columns == 0 )
               $output .= '<br style="clear: both" />';
     }
} else {
     $link = isset($attr['link']) && 'file' == $attr['link'] ?
          wp_get_attachment_link($id, $size, false, false) :
          wp_get_attachment_link($id, $size, true, false);

     $output .= "<{$itemtag} class='gallery-item'>";
     $output .= "
     <{$icontag} class='gallery-icon'>
     $link
     </{$icontag}>";
     if ( $captiontag && trim($attachment->post_excerpt) ) {
          $output .= "
          <{$captiontag} class='gallery-caption'>
          " . wptexturize($attachment->post_excerpt) . "
          </{$captiontag}>";
     }
     $output .= "</{$itemtag}>";
     if ( $columns > 0 && ++$i % $columns == 0 )
          $output .= '<br style="clear: both" />';
}

// ========================== end anh hack =============================================

This second part looks complicated, but I really didn’t add anything fancy. All it does is check if $listfilegroup is set, and if it is, it runs through the default display algorithm for only those media whose description (stored as post_content) matches $listfilegroup. (The default algorithm is the command part I had commented out; perhaps there’s a way to do this without commenting it out, but the above implementation seems the simplest to me.) If $listfilegroup is not set, then it runs through the default display algorithm for all media in $attachments (which are probably all the images).

So, the usage is the same as for yesterday’s hack: just use the gallery shortcode and set the linklist option to a gallery identifier, e.g., . Then use that identifier as the description for all files to be included together that gallery.

What if you want to use a gallery file in more than one gallery? Well, for images, you can try this multiple galleries plugin – I haven’t tried it myself, but it seems straightforward with a simple GUI change. For non-image file types, with the hacks I’ve described above, you could add the file again and give it a different description, so that each can be displayed in a gallery.

Thoughts? Always room for improvement.

4 thoughts on “Make WP gallery display non-image files, independent of file type

  1. hi startac, thanks very much for your feedback – i’m definitely more motivated to work on a plugin if people would actually find it useful. meanwhile, please let me know if i can help you with implementing the hack. thanks again!

  2. i just updated the wp-includes/media.php in wordpress v3.1. it’s nearly identical to the example above for wp v2.8.6. HOWEVER, it seems to have caused a problem with the admin interface of pages using the adapted gallery shortcode: only HTML view is available. i have no idea why! i’ll update if the problem is resolved somehow.

  3. Anh,

    Put me down for making this a plugin. Sorry if I didn’t read your post carefully enough, but does your hack create a thumbnail?

    Here is my current workflow for a PDF gallery. I hate it:
    Open PDF in photoshop, save thumbnail
    upload PDF to media library.
    Upload thumbnail to gallery. Post hyperlink to PDF in caption field

    Would be so much nicer to just upload the PDF into a gallery, create its caption, and choose which page of the PDF gets thumbnailed.

    Somebody on wordpress.org asked if nextGEN gallery could be modded to accept PDFs. People that replied in the thread couldn’t see the point, but I am certain many people would find this very useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>