Created
          July 26, 2019 13:53 
        
      - 
      
- 
        Save lilumi/55969e425146baa7949875b80681733f to your computer and use it in GitHub Desktop. 
    Convert standart Gallery to bootstrap format
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| add_filter('post_gallery', 'lm_bootstrap_gallery', 10, 3); | |
| // add valid bootstrap classes | |
| function lm_bootstrap_gallery($output = '' , $attr, $instance ) { | |
| $post = get_post(); | |
| $atts = shortcode_atts( | |
| array( | |
| 'order' => 'ASC', | |
| 'orderby' => 'menu_order ID', | |
| 'id' => $post ? $post->ID : 0, | |
| 'itemtag' => 'figure' , | |
| 'icontag' => 'div' , | |
| 'captiontag' => 'figcaption', | |
| 'columns' => 2, | |
| 'size' => 'thumbnail', | |
| 'include' => '', | |
| 'exclude' => '', | |
| 'link' => '', | |
| ), | |
| $attr, | |
| 'gallery' | |
| ); | |
| $id = intval( $atts['id'] ); | |
| if ( ! empty( $atts['include'] ) ) { | |
| $_attachments = get_posts( | |
| array( | |
| 'include' => $atts['include'], | |
| 'post_status' => 'inherit', | |
| 'post_type' => 'attachment', | |
| 'post_mime_type' => 'image', | |
| 'order' => $atts['order'], | |
| 'orderby' => $atts['orderby'], | |
| ) | |
| ); | |
| $attachments = array(); | |
| foreach ( $_attachments as $key => $val ) { | |
| $attachments[ $val->ID ] = $_attachments[ $key ]; | |
| } | |
| } elseif ( ! empty( $atts['exclude'] ) ) { | |
| $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'], | |
| ) | |
| ); | |
| } else { | |
| $attachments = get_children( | |
| array( | |
| 'post_parent' => $id, | |
| 'post_status' => 'inherit', | |
| 'post_type' => 'attachment', | |
| 'post_mime_type' => 'image', | |
| 'order' => $atts['order'], | |
| 'orderby' => $atts['orderby'], | |
| ) | |
| ); | |
| } | |
| if ( empty( $attachments ) ) { | |
| return ''; | |
| } | |
| if ( is_feed() ) { | |
| $output = "\n"; | |
| foreach ( $attachments as $att_id => $attachment ) { | |
| $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n"; | |
| } | |
| return $output; | |
| } | |
| $itemtag = tag_escape( $atts['itemtag'] ); | |
| $captiontag = tag_escape( $atts['captiontag'] ); | |
| $icontag = tag_escape( $atts['icontag'] ); | |
| $valid_tags = wp_kses_allowed_html( 'post' ); | |
| if ( ! isset( $valid_tags[ $itemtag ] ) ) { | |
| $itemtag = 'dl'; | |
| } | |
| if ( ! isset( $valid_tags[ $captiontag ] ) ) { | |
| $captiontag = 'dd'; | |
| } | |
| if ( ! isset( $valid_tags[ $icontag ] ) ) { | |
| $icontag = 'dt'; | |
| } | |
| $columns = intval( $atts['columns'] ); | |
| $column_classes = array('col-md', 'col-md', 'col-sm-6', 'col-md-6 col-lg', 'col-md-6 col-lg', 'col-md-6 col-lg', 'col-md-6 col-lg' ); | |
| $item_class = 'col-sm'; //$column_classes[$columns]; | |
| $float = is_rtl() ? 'right' : 'left'; | |
| $selector = "gallery-{$instance}"; | |
| $size_class = sanitize_html_class( $atts['size'] ); | |
| $output = "<div id='$selector' class='row gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; | |
| $i = 0; | |
| foreach ( $attachments as $id => $attachment ) { | |
| $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : ''; | |
| if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { | |
| $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); | |
| } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) { | |
| $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr ); | |
| } else { | |
| $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); | |
| } | |
| $image_meta = wp_get_attachment_metadata( $id ); | |
| $orientation = ''; | |
| if ( isset( $image_meta['height'], $image_meta['width'] ) ) { | |
| $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; | |
| } | |
| $output .= "<{$itemtag} class='gallery-item {$item_class}'>"; | |
| $output .= " | |
| <{$icontag} class='gallery-icon {$orientation}'> | |
| $image_output | |
| </{$icontag}>"; | |
| if ( $captiontag && trim( $attachment->post_excerpt ) ) { | |
| $output .= " | |
| <{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'> | |
| " . wptexturize( $attachment->post_excerpt ) . " | |
| </{$captiontag}>"; | |
| } | |
| $output .= "</{$itemtag}>"; | |
| } | |
| $output .= " | |
| </div>\n"; | |
| return $output; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment