Advertisement
alchymyth

gallery caption link

May 27th, 2011
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.87 KB | None | 0 0
  1. //turn gallery caption text into link, linking to the same location as the image//
  2. remove_shortcode('gallery', 'gallery_shortcode');
  3. add_shortcode('gallery', 'captionlink_gallery_shortcode');
  4.  
  5. /**
  6.  * The Gallery shortcode. //direct copy from /wp-includes/media.php from line 745//
  7.  *
  8.  * This implements the functionality of the Gallery Shortcode for displaying
  9.  * WordPress images on a post.
  10.  *
  11.  * @since 2.5.0
  12.  *
  13.  * @param array $attr Attributes attributed to the shortcode.
  14.  * @return string HTML content to display gallery.
  15.  */
  16. function captionlink_gallery_shortcode($attr) {
  17.     global $post, $wp_locale;
  18.  
  19.     static $instance = 0;
  20.     $instance++;
  21.  
  22.     // Allow plugins/themes to override the default gallery template.
  23.     $output = apply_filters('post_gallery', '', $attr);
  24.     if ( $output != '' )
  25.         return $output;
  26.  
  27.     // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  28.     if ( isset( $attr['orderby'] ) ) {
  29.         $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  30.         if ( !$attr['orderby'] )
  31.             unset( $attr['orderby'] );
  32.     }
  33.  
  34.     extract(shortcode_atts(array(
  35.         'order'      => 'ASC',
  36.         'orderby'    => 'menu_order ID',
  37.         'id'         => $post->ID,
  38.         'itemtag'    => 'dl',
  39.         'icontag'    => 'dt',
  40.         'captiontag' => 'dd',
  41.         'columns'    => 3,
  42.         'size'       => 'thumbnail',
  43.         'include'    => '',
  44.         'exclude'    => ''
  45.     ), $attr));
  46.  
  47.     $id = intval($id);
  48.     if ( 'RAND' == $order )
  49.         $orderby = 'none';
  50.  
  51.     if ( !empty($include) ) {
  52.         $include = preg_replace( '/[^0-9,]+/', '', $include );
  53.         $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  54.  
  55.         $attachments = array();
  56.         foreach ( $_attachments as $key => $val ) {
  57.             $attachments[$val->ID] = $_attachments[$key];
  58.         }
  59.     } elseif ( !empty($exclude) ) {
  60.         $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  61.         $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  62.     } else {
  63.         $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  64.     }
  65.  
  66.     if ( empty($attachments) )
  67.         return '';
  68.  
  69.     if ( is_feed() ) {
  70.         $output = "\n";
  71.         foreach ( $attachments as $att_id => $attachment )
  72.             $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  73.         return $output;
  74.     }
  75.  
  76.     $itemtag = tag_escape($itemtag);
  77.     $captiontag = tag_escape($captiontag);
  78.     $columns = intval($columns);
  79.     $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  80.     $float = is_rtl() ? 'right' : 'left';
  81.  
  82.     $selector = "gallery-{$instance}";
  83.  
  84.     $gallery_style = $gallery_div = '';
  85.     if ( apply_filters( 'use_default_gallery_style', true ) )
  86.         $gallery_style = "
  87.         <style type='text/css'>
  88.             #{$selector} {
  89.                 margin: auto;
  90.             }
  91.             #{$selector} .gallery-item {
  92.                 float: {$float};
  93.                 margin-top: 10px;
  94.                 text-align: center;
  95.                 width: {$itemwidth}%;
  96.             }
  97.             #{$selector} img {
  98.                 border: 2px solid #cfcfcf;
  99.             }
  100.             #{$selector} .gallery-caption {
  101.                 margin-left: 0;
  102.             }
  103.         </style>
  104.         <!-- see gallery_shortcode() in wp-includes/media.php -->";
  105.     $size_class = sanitize_html_class( $size );
  106.     $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  107.     $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
  108.  
  109.     $i = 0;
  110.     foreach ( $attachments as $id => $attachment ) {
  111.         $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  112.  
  113.         $output .= "<{$itemtag} class='gallery-item'>";
  114.         $output .= "
  115.             <{$icontag} class='gallery-icon'>
  116.                 $link
  117.             </{$icontag}>";
  118.         if ( $captiontag && trim($attachment->post_excerpt) ) {
  119.        
  120.         ////EDIT START/by alchymyth/
  121.         preg_match("#(href=\')(.*)(\' title)#e",$link,$link_url); //extract any link from the image
  122.         //print_r($link_url);
  123.         if( $link_url[2] ) :
  124.                
  125.             $output .= "
  126.                 <{$captiontag} class='wp-caption-text gallery-caption'>
  127.                 <a href='$link_url[2]'>" . wptexturize($attachment->post_excerpt) . "</a>
  128.                 </{$captiontag}>";             
  129.        
  130.         else :
  131.         ////EDIT END
  132.                
  133.             $output .= "
  134.                 <{$captiontag} class='wp-caption-text gallery-caption'>
  135.                 " . wptexturize($attachment->post_excerpt) . "
  136.                 </{$captiontag}>";
  137.                
  138.         endif; ////EDIT SINGLE LINE
  139.        
  140.         }
  141.         $output .= "</{$itemtag}>";
  142.         if ( $columns > 0 && ++$i % $columns == 0 )
  143.             $output .= '<br style="clear: both" />';
  144.     }
  145.  
  146.     $output .= "
  147.             <br style='clear: both;' />
  148.         </div>\n";
  149.  
  150.     return $output;
  151. }
  152. //////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement