SHARE
TWEET

(WordPress) my_post_gallery()

a guest Oct 22nd, 2018 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // See gallery_shortcode() for reference.
  3. add_filter( 'post_gallery', 'my_post_gallery', 10, 3 );
  4. function my_post_gallery( $output, $attr, $instance ) {
  5.     $post = get_post();
  6.  
  7.     // Change #1: I commented out this part.
  8.     /*static $instance = 0;
  9.     $instance++;*/
  10.  
  11.     if ( ! empty( $attr['ids'] ) ) {
  12.         // 'ids' is explicitly ordered, unless you specify otherwise.
  13.         if ( empty( $attr['orderby'] ) ) {
  14.             $attr['orderby'] = 'post__in';
  15.         }
  16.         $attr['include'] = $attr['ids'];
  17.     }
  18.  
  19.     // Change #2: I commented out this part.
  20.     /*$output = apply_filters( 'post_gallery', '', $attr, $instance );
  21.     if ( $output != '' ) {
  22.         return $output;
  23.     }*/
  24.  
  25.     $html5 = current_theme_supports( 'html5', 'gallery' );
  26.     $atts = shortcode_atts( array(
  27.         'order'      => 'ASC',
  28.         'orderby'    => 'menu_order ID',
  29.         'id'         => $post ? $post->ID : 0,
  30.         'itemtag'    => $html5 ? 'figure'     : 'dl',
  31.         'icontag'    => $html5 ? 'div'        : 'dt',
  32.         'captiontag' => $html5 ? 'figcaption' : 'dd',
  33.         'columns'    => 3,
  34.         'size'       => 'thumbnail',
  35.         'include'    => '',
  36.         'exclude'    => '',
  37.         'link'       => ''
  38.     ), $attr, 'gallery' );
  39.  
  40.     $id = intval( $atts['id'] );
  41.  
  42.     if ( ! empty( $atts['include'] ) ) {
  43.         $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  44.  
  45.         $attachments = array();
  46.         foreach ( $_attachments as $key => $val ) {
  47.             $attachments[$val->ID] = $_attachments[$key];
  48.         }
  49.     } elseif ( ! empty( $atts['exclude'] ) ) {
  50.         $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  51.     } else {
  52.         $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  53.     }
  54.  
  55.     if ( empty( $attachments ) ) {
  56.         return '';
  57.     }
  58.  
  59.     if ( is_feed() ) {
  60.         $output = "\n";
  61.         foreach ( $attachments as $att_id => $attachment ) {
  62.             $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
  63.         }
  64.         return $output;
  65.     }
  66.  
  67.     $itemtag = tag_escape( $atts['itemtag'] );
  68.     $captiontag = tag_escape( $atts['captiontag'] );
  69.     $icontag = tag_escape( $atts['icontag'] );
  70.     $valid_tags = wp_kses_allowed_html( 'post' );
  71.     if ( ! isset( $valid_tags[ $itemtag ] ) ) {
  72.         $itemtag = 'dl';
  73.     }
  74.     if ( ! isset( $valid_tags[ $captiontag ] ) ) {
  75.         $captiontag = 'dd';
  76.     }
  77.     if ( ! isset( $valid_tags[ $icontag ] ) ) {
  78.         $icontag = 'dt';
  79.     }
  80.  
  81.     $columns = intval( $atts['columns'] );
  82.     $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  83.     $float = is_rtl() ? 'right' : 'left';
  84.  
  85.     $selector = "gallery-{$instance}";
  86.  
  87.     $gallery_style = '';
  88.  
  89.     /**
  90.      * Filters whether to print default gallery styles.
  91.      *
  92.      * @since 3.1.0
  93.      *
  94.      * @param bool $print Whether to print default gallery styles.
  95.      *                    Defaults to false if the theme supports HTML5 galleries.
  96.      *                    Otherwise, defaults to true.
  97.      */
  98.     if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
  99.         $gallery_style = "
  100.         <style type='text/css'>
  101.             #{$selector} {
  102.                 margin: auto;
  103.             }
  104.             #{$selector} .gallery-item {
  105.                 float: {$float};
  106.                 margin-top: 10px;
  107.                 text-align: center;
  108.                 width: {$itemwidth}%;
  109.             }
  110.             #{$selector} img {
  111.                 border: 2px solid #cfcfcf;
  112.             }
  113.             #{$selector} .gallery-caption {
  114.                 margin-left: 0;
  115.             }
  116.             /* see gallery_shortcode() in wp-includes/media.php */
  117.         </style>\n\t\t";
  118.     }
  119.  
  120.     $size_class = sanitize_html_class( $atts['size'] );
  121.     $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  122.  
  123.     /**
  124.      * Filters the default gallery shortcode CSS styles.
  125.      *
  126.      * @since 2.5.0
  127.      *
  128.      * @param string $gallery_style Default CSS styles and opening HTML div container
  129.      *                              for the gallery shortcode output.
  130.      */
  131.     $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div );
  132.  
  133.     $i = 0;
  134.     foreach ( $attachments as $id => $attachment ) {
  135.  
  136.         $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : '';
  137.         if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
  138.             $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
  139.         } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
  140.             $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
  141.         } else {
  142.             $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
  143.         }
  144.         $image_meta  = wp_get_attachment_metadata( $id );
  145.  
  146.         $orientation = '';
  147.         if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
  148.             $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
  149.         }
  150.         $output .= "<{$itemtag} class='gallery-item'>";
  151.         $output .= "
  152.             <{$icontag} class='gallery-icon {$orientation}'>
  153.                 $image_output
  154.             </{$icontag}>";
  155.         if ( $captiontag && trim($attachment->post_excerpt) ) {
  156.             $output .= "
  157.                 <{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'>
  158.                 " . wptexturize($attachment->post_excerpt) . "
  159.                 </{$captiontag}>";
  160.         }
  161.         $output .= "</{$itemtag}>";
  162.         // Change #3: I commented out this code.
  163.         /*if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) {
  164.             $output .= '<br style="clear: both" />';
  165.         }*/
  166.     }
  167.  
  168.     // Change #4: I commented out this code.
  169.     /*if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) {
  170.         $output .= "
  171.             <br style='clear: both' />";
  172.     }*/
  173.  
  174.     // Change #5: Here's the only one `br` tag we need.
  175.     $output .= "
  176.             <br style='clear: both' />
  177.         </div>\n";
  178.  
  179.     return $output;
  180. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top