Advertisement
Guest User

Untitled

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