Revers to old gallery because add_shortcode is not allowed

This commit is contained in:
koenemann 2016-11-21 11:41:29 +01:00
parent 712f1d0475
commit 018e930d44
1 changed files with 110 additions and 86 deletions

View File

@ -1,105 +1,129 @@
<?php <?php
//Based on Roots Sage Gallery: https://github.com/roots/sage/blob/5b9786b8ceecfe717db55666efe5bcf0c9e1801c/lib/gallery.php //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/
// Custom filter function to modify default gallery shortcode output
function bootstrap_wp_gallery( $output, $attr ) {
// Remove built in shortcode
remove_shortcode('gallery', 'gallery_shortcode');
// Replace with custom shortcode // Initialize
function shortcode_gallery($attr) { global $post, $wp_locale;
$post = get_post();
// Gallery instance counter
static $instance = 0; static $instance = 0;
$instance++; $instance++;
if (!empty($attr['ids'])) {
if (empty($attr['orderby'])) { // Validate the author's orderby attribute
$attr['orderby'] = 'post__in'; if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( ! $attr['orderby'] ) unset( $attr['orderby'] );
} }
$attr['include'] = $attr['ids'];
} // Get attributes from shortcode
$output = apply_filters('post_gallery', '', $attr); extract( shortcode_atts( array(
if ($output != '') {
return $output;
}
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby']) {
unset($attr['orderby']);
}
}
extract(shortcode_atts([
'order' => 'ASC', 'order' => 'ASC',
'orderby' => 'menu_order ID', 'orderby' => 'menu_order ID',
'id' => $post->ID, 'id' => $post->ID,
'itemtag' => '', 'itemtag' => 'div',
'icontag' => '', 'icontag' => 'div',
'captiontag' => '', 'captiontag' => 'div',
'columns' => 3, 'columns' => 3,
'size' => 'thumbnail', 'size' => 'thumbnail',
'include' => '', 'exclude' => ''
'exclude' => '', ), $attr ) );
'link' => ''
], $attr)); // Initialize
$id = intval($id); $id = intval( $id );
$columns = (12 % $columns == 0) ? $columns : 3; $attachments = array();
$grid = sprintf('col-sm-%1$s col-lg-%1$s', 12 / $columns); if ( $order == 'RAND' ) $orderby = 'none';
if ($order === 'RAND') {
$orderby = 'none';
} if ( ! empty( $include ) ) {
if (!empty($include)) {
$_attachments = get_posts(['include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby]); // Include attribute is present
$attachments = []; $include = preg_replace( '/[^0-9,]+/', '', $include );
foreach ($_attachments as $key => $val) { $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
$attachments[$val->ID] = $_attachments[$key];
} // Setup attachments array
} elseif (!empty($exclude)) { foreach ( $_attachments as $key => $val ) {
$attachments = get_children(['post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby]); $attachments[ $val->ID ] = $_attachments[ $key ];
} else { }
$attachments = get_children(['post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby]);
} } else if ( ! empty( $exclude ) ) {
if (empty($attachments)) {
return ''; // Exclude attribute is present
} $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
if (is_feed()) {
$output = "\n"; // Setup attachments array
foreach ($attachments as $att_id => $attachment) { $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
$output .= wp_get_attachment_link($att_id, $size, true) . "\n"; } else {
} // Setup attachments array
return $output; $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
} }
$unique = (get_query_var('page')) ? $instance . '-p' . get_query_var('page') : $instance;
$output = '<div class="gallery gallery-' . $id . '-' . $unique . '">'; if ( empty( $attachments ) ) return '';
$i = 0;
foreach ($attachments as $id => $attachment) { // Filter gallery differently for feeds
switch($link) { if ( is_feed() ) {
case 'file': $output = "\n";
$image = wp_get_attachment_link($id, $size, false, false); foreach ( $attachments as $att_id => $attachment ) $output .= wp_get_attachment_link( $att_id, $size, true ) . "\n";
break; return $output;
case 'none': }
$image = wp_get_attachment_image($id, $size, false, ['class' => 'thumbnail img-thumbnail']);
break; // Filter tags and attributes
default: $itemtag = tag_escape( $itemtag );
$image = wp_get_attachment_link($id, $size, true, false); $captiontag = tag_escape( $captiontag );
break; $columns = intval( $columns );
} $itemwidth = $columns > 0 ? floor( 12 / $columns ) : 100;
$output .= ($i % $columns == 0) ? '<div class="row gallery-row">' : ''; $float = is_rtl() ? 'right' : 'left';
$output .= '<div class="' . $grid .'">' . $image; $selector = "gallery-{$instance}";
if (trim($attachment->post_excerpt)) {
$output .= '<div class="caption hidden">' . wptexturize($attachment->post_excerpt) . '</div>'; // Filter gallery CSS
} $output = apply_filters( 'gallery_style', "
$output .= '</div>';
$i++; <div class='gallery galleryid-{$id} row' id='$selector'>"
$output .= ($i % $columns == 0) ? '</div>' : ''; );
}
$output .= ($i % $columns != 0 ) ? '</div>' : ''; // Iterate through the attachments in this gallery instance
$output .= '</div>'; $i = 0;
return $output; foreach ( $attachments as $id => $attachment ) {
}
add_shortcode('gallery', 'shortcode_gallery'); // 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 );
// Start itemtag
$output .= "<{$itemtag} class='gallery-item col-md-{$itemwidth}'>";
// icontag
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim( $attachment->post_excerpt ) ) {
// captiontag
$output .= "
<{$captiontag} class='gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
// End itemtag
$output .= "</{$itemtag}>";
// Line breaks by columns set
if($columns > 0 && ++$i % $columns == 0) $output .= '<br style="clear: both">';
}
// End gallery output
$output .= "
<br style='clear: both;'>
</div>\n";
return $output;
/**
* Add class="thumbnail img-thumbnail" to attachment items
*/
function attachment_link_class($html) {
$html = str_replace('<a', '<a class="thumbnail img-thumbnail"', $html);
return $html;
} }
add_filter('wp_get_attachment_link', 'attachment_link_class', 10, 1);
// Apply filter to default gallery shortcode
add_filter( 'post_gallery', 'bootstrap_wp_gallery', 10, 2 );