Advertisement
brewern

Custom URL for WP Gallery

Apr 26th, 2011
1,861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.88 KB | None | 0 0
  1. <?php
  2. /* -------------------------------
  3. CUSTOM LINK PER GALLERY IMAGE
  4. ------------------------------- */
  5. function rt_image_attachment_fields_to_edit($form_fields, $post) {
  6.   $form_fields["rt-image-link"] = array(
  7.     "label" => __("Custom Link"),
  8.     "input" => "text", // default
  9.     "value" => get_post_meta($post->ID, "_rt-image-link", true),
  10.     //"helps" => __("To be used with special slider added via [rt_carousel] shortcode."),
  11.   );
  12.   return $form_fields;
  13. }
  14. add_filter("attachment_fields_to_edit", "rt_image_attachment_fields_to_edit", null, 2);
  15.  
  16. function rt_image_attachment_fields_to_save($post, $attachment) {
  17.   // $attachment part of the form $_POST ($_POST[attachments][postID])
  18.   // $post['post_type'] == 'attachment'
  19.   if( isset($attachment['rt-image-link']) ){
  20.     // update_post_meta(postID, meta_key, meta_value);
  21.     update_post_meta($post['ID'], '_rt-image-link', $attachment['rt-image-link']);
  22.   }
  23.   return $post;
  24. }
  25. add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);
  26.  
  27. /* -------------------------------
  28. MODIFIED CORE FUNCTION
  29. ------------------------------- */
  30. add_shortcode('gallery', 'geekee_gallery_shortcode');
  31.  
  32. /**
  33. * The Gallery shortcode.
  34. *
  35. * This implements the functionality of the Gallery Shortcode for displaying
  36. * WordPress images on a post.
  37. *
  38. * @since 2.5.0
  39. *
  40. * @param array $attr Attributes attributed to the shortcode.
  41. * @return string HTML content to display gallery.
  42. */
  43. function geekee_gallery_shortcode($attr) {
  44.   global $post, $wp_locale;
  45.  
  46.   static $instance = 0;
  47.   $instance++;
  48.  
  49.   // Allow plugins/themes to override the default gallery template.
  50.   $output = apply_filters('post_gallery', '', $attr);
  51.   if ( $output != '' )
  52.     return $output;
  53.  
  54.   // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  55.   if ( isset( $attr['orderby'] ) ) {
  56.     $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  57.     if ( !$attr['orderby'] )
  58.       unset( $attr['orderby'] );
  59.   }
  60.  
  61.   extract(shortcode_atts(array(
  62.     'order'      => 'ASC',
  63.     'orderby'    => 'menu_order ID',
  64.     'id'         => $post->ID,
  65.     'itemtag'    => 'dl',
  66.     'icontag'    => 'dt',
  67.     'captiontag' => 'dd',
  68.     'columns'    => 3,
  69.     'size'       => 'thumbnail',
  70.     'include'    => '',
  71.     'exclude'    => ''
  72.   ), $attr));
  73.  
  74.   $id = intval($id);
  75.   if ( 'RAND' == $order )
  76.     $orderby = 'none';
  77.  
  78.   if ( !empty($include) ) {
  79.     $include = preg_replace( '/[^0-9,]+/', '', $include );
  80.     $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  81.  
  82.     $attachments = array();
  83.     foreach ( $_attachments as $key => $val ) {
  84.       $attachments[$val->ID] = $_attachments[$key];
  85.     }
  86.   } elseif ( !empty($exclude) ) {
  87.     $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  88.     $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  89.   } else {
  90.     $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  91.   }
  92.  
  93.   if ( empty($attachments) )
  94.     return '';
  95.  
  96.   if ( is_feed() ) {
  97.     $output = "\n";
  98.     foreach ( $attachments as $att_id => $attachment )
  99.       $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  100.     return $output;
  101.   }
  102.  
  103.   $itemtag = tag_escape($itemtag);
  104.   $captiontag = tag_escape($captiontag);
  105.   $columns = intval($columns);
  106.   $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  107.   $float = is_rtl() ? 'right' : 'left';
  108.  
  109.   $selector = "gallery-{$instance}";
  110.  
  111.     $gallery_style = $gallery_div = '';
  112.     if ( apply_filters( 'use_default_gallery_style', true ) )
  113.         $gallery_style = "
  114.         <style type='text/css'>
  115.             #{$selector} {
  116.                 margin: auto;
  117.             }
  118.             #{$selector} .gallery-item {
  119.                 float: {$float};
  120.                 margin-top: 10px;
  121.                 text-align: center;
  122.                 width: {$itemwidth}%;
  123.             }
  124.             #{$selector} img {
  125.                 border: 2px solid #cfcfcf;
  126.             }
  127.             #{$selector} .gallery-caption {
  128.                 margin-left: 0;
  129.             }
  130.         </style>
  131.         <!-- see gallery_shortcode() in wp-includes/media.php -->";
  132.     $size_class = sanitize_html_class( $size );
  133.     $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  134.     $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
  135.  
  136.   $i = 0;
  137.   foreach ( $attachments as $id => $attachment ) {
  138.     $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  139.  
  140.   /* -------------------------------
  141.   MODIFICATION ################
  142.   ------------------------------- */
  143.   $image = wp_get_attachment_image($id, $size, false);
  144.   $attachment_meta = get_post_meta($id, '_rt-image-link', true);
  145.   if($attachment_meta){
  146.     if($attr['link'] == 'custom_url'){
  147.       $link = $attachment_meta;
  148.     }
  149.   }
  150.  
  151.   $output .= "<{$itemtag} class='gallery-item'>";
  152.   $output .= "<{$icontag} class='gallery-icon'>";
  153.  
  154.     /* -------------------------------
  155.   MODIFICATION ################
  156.   ------------------------------- */
  157.   if($attachment_meta){
  158.     $output .= "<a href='$link'>$image</a>";
  159.   } else {
  160.     $output .= $link;
  161.   }
  162.  
  163.   $output .= "</{$icontag}>";
  164.   if ( $captiontag && trim($attachment->post_excerpt) ) {
  165.     $output .= "<{$captiontag} class='gallery-caption'>" . wptexturize($attachment->post_excerpt) . "</{$captiontag}>";
  166.   }
  167.   $output .= "</{$itemtag}>";
  168.   if ( $columns > 0 && ++$i % $columns == 0 )
  169.     $output .= '<br style="clear: both;" />';
  170.   }
  171.  
  172.   $output .= "<br style='clear: both;' /></div>\n";
  173.  
  174.   return $output;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement