2016-05-17 15:41:00 +00:00
< ? php
2016-11-01 16:13:23 +00:00
//Based on Jeff Hays code and his article here: http://robido.com/wordpress/wordpress-gallery-filter-to-modify-the-html-output-of-the-default-gallery-shortcode-and-style/
2016-05-17 15:41:00 +00:00
// Custom filter function to modify default gallery shortcode output
function bootstrap_wp_gallery ( $output , $attr ) {
2016-11-01 16:13:23 +00:00
// Initialize
global $post , $wp_locale ;
// Gallery instance counter
static $instance = 0 ;
$instance ++ ;
// Validate the author's orderby attribute
if ( isset ( $attr [ 'orderby' ] ) ) {
$attr [ 'orderby' ] = sanitize_sql_orderby ( $attr [ 'orderby' ] );
if ( ! $attr [ 'orderby' ] ) unset ( $attr [ 'orderby' ] );
}
// Get attributes from shortcode
extract ( shortcode_atts ( array (
'order' => 'ASC' ,
'orderby' => 'menu_order ID' ,
'id' => $post -> ID ,
'itemtag' => 'div' ,
'icontag' => 'div' ,
'captiontag' => 'div' ,
'columns' => 3 ,
'size' => 'thumbnail' ,
'exclude' => ''
), $attr ) );
// Initialize
$id = intval ( $id );
$attachments = array ();
if ( $order == 'RAND' ) $orderby = 'none' ;
if ( ! empty ( $include ) ) {
// Include attribute is present
$include = preg_replace ( '/[^0-9,]+/' , '' , $include );
$_attachments = get_posts ( array ( 'include' => $include , 'post_status' => 'inherit' , 'post_type' => 'attachment' , 'post_mime_type' => 'image' , 'order' => $order , 'orderby' => $orderby ) );
// Setup attachments array
foreach ( $_attachments as $key => $val ) {
$attachments [ $val -> ID ] = $_attachments [ $key ];
}
} else if ( ! empty ( $exclude ) ) {
// Exclude attribute is present
$exclude = preg_replace ( '/[^0-9,]+/' , '' , $exclude );
// Setup attachments array
$attachments = get_children ( array ( 'post_parent' => $id , 'exclude' => $exclude , 'post_status' => 'inherit' , 'post_type' => 'attachment' , 'post_mime_type' => 'image' , 'order' => $order , 'orderby' => $orderby ) );
} else {
// Setup attachments array
$attachments = get_children ( array ( 'post_parent' => $id , 'post_status' => 'inherit' , 'post_type' => 'attachment' , 'post_mime_type' => 'image' , 'order' => $order , 'orderby' => $orderby ) );
}
if ( empty ( $attachments ) ) return '' ;
// Filter gallery differently for feeds
if ( is_feed () ) {
$output = " \n " ;
foreach ( $attachments as $att_id => $attachment ) $output .= wp_get_attachment_link ( $att_id , $size , true ) . " \n " ;
return $output ;
}
// Filter tags and attributes
$itemtag = tag_escape ( $itemtag );
$captiontag = tag_escape ( $captiontag );
$columns = intval ( $columns );
$itemwidth = $columns > 0 ? floor ( 12 / $columns ) : 100 ;
$float = is_rtl () ? 'right' : 'left' ;
$selector = " gallery- { $instance } " ;
2016-05-17 15:41:00 +00:00
// Filter gallery CSS
2016-11-01 16:13:23 +00:00
$output = apply_filters ( 'gallery_style' , "
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
< div class = 'gallery galleryid-{$id} row' id = '$selector' > "
);
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// Iterate through the attachments in this gallery instance
$i = 0 ;
foreach ( $attachments as $id => $attachment ) {
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// Attachment link
$link = isset ( $attr [ 'link' ] ) && 'file' == $attr [ 'link' ] ? wp_get_attachment_link ( $id , $size , false , false ) : wp_get_attachment_link ( $id , $size , true , false );
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// Start itemtag
$output .= " < { $itemtag } class='gallery-item col-md- { $itemwidth } '> " ;
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// icontag
$output .= "
< { $icontag } class = 'gallery-icon' >
$link
</ { $icontag } > " ;
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
if ( $captiontag && trim ( $attachment -> post_excerpt ) ) {
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// captiontag
$output .= "
< { $captiontag } class = 'gallery-caption' >
" . wptexturize( $attachment->post_excerpt ) . "
</ { $captiontag } > " ;
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
}
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// End itemtag
$output .= " </ { $itemtag } > " ;
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// Line breaks by columns set
if ( $columns > 0 && ++ $i % $columns == 0 ) $output .= '<br style="clear: both">' ;
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
}
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
// End gallery output
$output .= "
< br style = 'clear: both;' >
</ div > \n " ;
2016-05-17 15:41:00 +00:00
2016-11-01 16:13:23 +00:00
return $output ;
2016-05-17 15:41:00 +00:00
}
// Apply filter to default gallery shortcode
add_filter ( 'post_gallery' , 'bootstrap_wp_gallery' , 10 , 2 );