Wordpress - WP post gallery code cleaner
<?
function cleaner_gallery($output, $attr) {
$base_class = 'c-content-gallery';
if ( is_feed() ) {
return $output;
}
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] ) {
unset( $attr['orderby'] );
}
}
$defaults = array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => get_the_ID(),
'link' => '',
'itemtag' => 'figure',
'icontag' => 'dt',
'captiontag' => 'figcaption',
'columns' => 10,
'size' => 'thumbnail',
'ids' => '',
'include' => '',
'exclude' => '',
'numberposts' => -1,
'offset' => ''
);
$attr = shortcode_atts($defaults, $attr);
extract($attr);
$id = intval($id);
$children = array(
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby,
'exclude' => $exclude,
'include' => $include,
'numberposts' => $numberposts,
'offset' => $offset,
'suppress_filters' => true
);
if (empty($include)) {
$attachments = get_children(array_merge(array('post_parent' => $id), $children));
} else {
$attachments = get_posts($children);
}
if (empty($attachments)) {
return '<!-- Here be dragons but no images. -->';
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$size_class = sanitize_html_class($size);
$output = '<ul id="gallery-' . $id . '" class="s-textcontent__gallery ' . $base_class . ' u-size-' . $size_class . '">';
foreach ($attachments as $attachment) {
$output .= '<li class="' . $base_class . '__item"><' . $itemtag . ' class="' . $base_class . '__item-inner">';
$image = ( ( isset( $attr['link'] ) && 'file' == $attr['link'] ) ? wp_get_attachment_link( $attachment->ID, $size, false, false, false, array('class' => $base_class . '__image') ) : wp_get_attachment_link( $attachment->ID, $size, true, false, false, array('class' => $base_class . '__image') ) );
$output .= $image;
$title = wptexturize($attachment->post_title);
$caption = wptexturize($attachment->post_excerpt);
if (!empty($caption) || !empty($title)) {
$output .= '<' . $captiontag . ' class="' . $base_class . '__caption">' . $title . (($title && $caption) ? '<br>' : '') . $caption . '</' . $captiontag . '>';
}
$output .= '</' . $itemtag . '></li>';
}
$output .= '</ul>';
return $output;
}
add_filter( 'post_gallery', 'cleaner_gallery', 10, 2 );