Return to Snippet

Revision: 22911
at January 25, 2010 16:01 by lancemonotone


Initial Code
// Registers our function to filter default gallery shortcode
remove_shortcode('gallery');
add_shortcode('gallery', 'sandbox_gallery');

// Function to filter the default gallery shortcode
function sandbox_gallery($attr) {
	global $post;

	static $instance = 0;
	$instance++;

	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
	if ( isset( $attr['orderby'] ) ) {
		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
		if ( !$attr['orderby'] )
			unset( $attr['orderby'] );
	}

	extract(shortcode_atts(array(
		'order'      => 'ASC',
		'orderby'    => 'menu_order ID',
		'id'         => $post->ID,
		'itemtag'    => 'dl',
		'icontag'    => 'dt',
		'captiontag' => 'dd',
		'columns'    => 3,
		'size'       => 'thumbnail',
		'include'    => '',
		'exclude'    => ''
		), $attr));

	$id = intval($id);
	if ( 'RAND' == $order )
		$orderby = 'none';

	if ( !empty($include) ) {
		$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) );

		$attachments = array();
		foreach ( $_attachments as $key => $val ) {
			$attachments[$val->ID] = $_attachments[$key];
		}
	} elseif ( !empty($exclude) ) {
		$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
		$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
	} else {
		$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 '';

	if ( is_feed() ) {
		$output = "\n";
		foreach ( $attachments as $att_id => $attachment )
			$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
		return $output;
	}

	// check to see if tags have been set to false. If they are the defaults or have been set to a string value use that as the tag.
	if ($itemtag) $itemtag = tag_escape($itemtag);
	if ($captiontag) $captiontag = tag_escape($captiontag);
	if ($icontag) $icontag = tag_escape($icontag);
	$columns = intval($columns);

	$selector = "gallery-{$instance}";

	$output = "<div id='$selector' class='gallery galleryid-{$id}'>\n";

	$i = 0;
	foreach ( $attachments as $id => $attachment ) {
	  ++$i;
		$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

		if ($itemtag) {
			$output .= '<'.$itemtag.' class="gallery-item ';
		  if( $columns > 0 && $i % $columns == 0 ) $output .= " last";
		  $output .= '">';
		}
		if ($icontag) $output .= "\n\t<" .$icontag. ">\t";
		$output .=  "\n\t".$link;
		if ($icontag) $output .= "\n\t</".$icontag.">";
		// if the attachment has a caption set
		if ( trim($attachment->post_excerpt) ) {
		  if ($captiontag) $output .= "\n<" .$captiontag. ">\n\t";
		  $output .= wptexturize($attachment->post_excerpt);
		  if ($captiontag) $output .= "\n</" .$captiontag. ">" . "<!-- end caption -->\n";
		}
		if ($itemtag) $output .= "\n</".$itemtag ."><!-- end itemtag -->\n";
		if ( $columns > 0 && $i % $columns == 0 ) $output .= "\n";
	}

	$output .= "</div><!-- end gallery -->\n";

	return $output;
}

Initial URL
http://368design.com

Initial Description
This is a combination of two fixes from Michael at [WPEngineer.com](http://wpengineer.com/a-solution-for-the-wordpress-gallery "WPEngineer.com"), which removes the styles from the gallery output, and a follow-up comment by [Aaron Cimolini](http://wpengineer.com/a-solution-for-the-wordpress-gallery/#comment-2070), which allows the developer to omit shortcode attributes.  

The blog stripped some of the usable code from the comment, so I cleaned it up and reposted it here.  I'm using it with the Sandbox theme, so I've replaced the original sandbox_gallery function with Michael and Aaron's improvement.

Initial Title
Wordpress 2.7+ Gallery Styles Fix

Initial Tags
wordpress

Initial Language
PHP