Advertisement
brentini

Add thickbox to wordpress images

Mar 2nd, 2012
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2. // adds thickbox to all images embedded in a post from the media library including galleries
  3. add_filter('the_content', 'brentini_addthickboxclass');
  4. function brentini_addthickboxclass($content) {
  5. add_thickbox();
  6.     $pattern[0] ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
  7.     $replacement[0] = '<a$1href=$2$3.$4$5 class="thickbox">';
  8.     $pattern[1]     = "/(<a href=)('|\")([^\>]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?)(>)(.*?) title=('|\")(.*?)('|\")(.*?)(<\/a>)/i";
  9.     $replacement[1] = '$1$2$3$4$5$6 title=$9$10$11$7$8 title=$9$10$11$12$13';
  10.     $pattern[2]     = "/(<a href=)('|\")([^\>]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?) title=([^\>]*?) title=([^\>]*?)(>)(.*?)(<\/a>)/i";
  11.     $replacement[2] = '$1$2$3$4$5$6 title=$7$9$10$11';
  12.     $content = preg_replace($pattern, $replacement, $content);
  13.     return $content;
  14. }
  15. function gallery_shortcode_tbi($attr) {
  16.     global $post;
  17.     if ( isset( $attr['orderby'] ) ) {
  18.         $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  19.         if ( !$attr['orderby'] )
  20.             unset( $attr['orderby'] );
  21.     }
  22.     extract(shortcode_atts(array(
  23.         'orderby'    => 'menu_order ASC, ID ASC',
  24.         'id'         => $post->ID,
  25.         'itemtag'    => 'dl',
  26.         'icontag'    => 'dt',
  27.         'captiontag' => 'dd',
  28.         'columns'    => 3,
  29.         'size'       => 'thumbnail',
  30.     ), $attr));
  31.     $id = intval($id);
  32.     $attachments = get_children("post_parent=$id&post_type=attachment&post_mime_type=image&orderby={$orderby}");
  33.     if ( empty($attachments) )
  34.         return '';
  35.     if ( is_feed() ) {
  36.         $output = "\n";
  37.         foreach ( $attachments as $id => $attachment )
  38.             $output .= wp_get_attachment_link($id, $size, true) . "\n";
  39.         return $output;
  40.     }
  41.     $itemtag = tag_escape($itemtag);
  42.     $captiontag = tag_escape($captiontag);
  43.     $columns = intval($columns);
  44.     $itemwidth = $columns > 0 ? floor(100/$columns) : 100; 
  45.     $output = apply_filters('gallery_style', "
  46.         <style type='text/css'>
  47.             .gallery {
  48.                 margin: auto;
  49.             }
  50.             .gallery-item {
  51.                 float: left;
  52.                 margin-top: 10px;
  53.                 text-align: center;
  54.                 width: {$itemwidth}%;           }
  55.             .gallery img {
  56.                 border: 2px solid #cfcfcf;
  57.             }
  58.             .gallery-caption {
  59.                 margin-left: 0;
  60.             }
  61.         </style>
  62.         <div class='gallery'>");
  63.     foreach ( $attachments as $id => $attachment ) {   
  64.         $a_img = wp_get_attachment_url($id);
  65.         $att_page = get_attachment_link($id);
  66.         $img = wp_get_attachment_image_src($id, $size);
  67.         $img = $img[0];
  68.         $title = $attachment->post_excerpt;
  69.         if($title == '') $title = $attachment->post_title;     
  70.         $output .= "<{$itemtag} class='gallery-item'>";
  71.         $output .= "
  72.             <{$icontag} class='gallery-icon'>          
  73.                     <a href=\"$a_img\" title=\"$title\" class=\"thickbox\" rel=\"gallery-$post->ID\">              
  74.                     <img src=\"$img\" alt=\"$title\" />            
  75.                     </a>                   
  76.             </{$icontag}>";
  77.         if ( $captiontag && trim($attachment->post_excerpt) ) {
  78.             $output .= "
  79.                 <{$captiontag} class='gallery-caption'>
  80.                 {$attachment->post_excerpt}
  81.                 </{$captiontag}>";
  82.         }
  83.         $output .= "</{$itemtag}>";
  84.         if ( $columns == 0 )
  85.             $output .= '<br style="clear: both" />';
  86.     }
  87.     $output .= "
  88.             <br style='clear: both;' />
  89.         </div>\n";
  90.     return $output;
  91. }
  92. remove_shortcode('gallery');
  93. add_shortcode('gallery', 'gallery_shortcode_tbi');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement