WordPress.org

Ready to get started?Download WordPress

Forums

Suggested improvement to gallery_shortcode (4 posts)

  1. hutorny
    Member
    Posted 4 years ago #

    To make image gallery easily integrated with Thumbnail Viewers gallery_shortcode function should be able to generate reference tag <a> with rel attribute.

    Suggested solution (additions indicated with !!!):
    Add rel attribute to the gallery shortcode and inject this attribute to the reference tag.

    function gallery_shortcode($attr) {
    	global $post;
    
    	static $instance = 0;
    	$instance++;
    
    	// Allow plugins/themes to override the default gallery template.
    	$output = apply_filters('post_gallery', '', $attr);
    	if ( $output != '' )
    		return $output;
    
    	// 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',
    		'rel'        => '', // !!! Add attribute
    		'captiontag' => 'dd',
    		'columns'    => 3,
    		'size'       => 'thumbnail'
    	), $attr));
    
    	$id = intval($id);
    	$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;
    	}
    
    	$itemtag = tag_escape($itemtag);
    	$captiontag = tag_escape($captiontag);
    	$columns = intval($columns);
    	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    
    	$selector = "gallery-{$instance}";
    
    	$output = apply_filters('gallery_style', "
    		<style type='text/css'>
    			#{$selector} {
    				margin: auto;
    			}
    			#{$selector} .gallery-item {
    				float: left;
    				margin-top: 10px;
    				text-align: center;
    				width: {$itemwidth}%;			}
    			#{$selector} img {
    				border: 2px solid #cfcfcf;
    			}
    			#{$selector} .gallery-caption {
    				margin-left: 0;
    			}
    		</style>
    		<!-- see gallery_shortcode() in wp-includes/media.php -->
    		<div id='$selector' class='gallery galleryid-{$id}'>");
    
    	$i = 0;
    	foreach ( $attachments as $id => $attachment ) {
    		$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
    		if( ! empty($rel) ) { !!! // Add rel injection
    			$link = str_replace('<a href=', "<a rel='$rel' href=", $link);
    	  }
    
    		$output .= "<{$itemtag} class='gallery-item'>";
    		$output .= "
    			<{$icontag} class='gallery-icon'>
    				$link
    			</{$icontag}>";
    		if ( $captiontag && trim($attachment->post_excerpt) ) {
    			$output .= "
    				<{$captiontag} class='gallery-caption'>
    				" . wptexturize($attachment->post_excerpt) . "
    				</{$captiontag}>";
    		}
    		$output .= "</{$itemtag}>";
    		if ( $columns > 0 && ++$i % $columns == 0 )
    			$output .= '<br style="clear: both" />';
    	}
    
    	$output .= "
    			<br style='clear: both;' />
    		</div>\n";
    
    	return $output;
    }
  2. luminopolis
    Member
    Posted 4 years ago #

    hutorny -

    THANK YOU THANK YOU THANK YOU THANK YOU

    this works perfect. it was very useful when hardwiring the rel="shadowbox" attr in to automatic gallery insertion (via do_shortcode())

    THANK YOU!!!

  3. thijs77
    Member
    Posted 4 years ago #

    i was looking for a way to do this, also using it with shadowbox, but could you please tell me where to put this code? it's not in the functions.php or shortcode.php apparently.

    thanks in advance!

    edit: i found it, wp-includes/media.php, but i get a bunch of errors on the page with the gallery, looking like this:

    array_map() [function.array-map]: The first argument, 'esc_attr', should be either NULL or a valid callback in ***/media.php on line 547

  4. mcnamee
    Member
    Posted 4 years ago #

    Genius!
    Wordpress please implement this in the next version. Very helpful for lightbox style things :)

Topic Closed

This topic has been closed to new replies.

About this Topic