Title: clean gallery shortcode
Last modified: August 19, 2016

---

# clean gallery shortcode

 *  [Edouard Duplessis](https://wordpress.org/support/users/eduplessis/)
 * (@eduplessis)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/clean-gallery-shortcode/)
 * I’ve modify the gallery shortcode to do a better and a cleaner job
 * here’s the code
    wp-includes/media.php line 384
 *     ```
       function gallery_shortcode($attr) {
       	global $post;
   
       	// 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'    => '',
       		'icontag'    => '',
       		'captiontag' => '',
       		'columns'    => 4,
       		'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 $id => $attachment )
       			$output .= wp_get_attachment_link($id, $size, true) . "\n";
       		return $output;
       	}
   
       	$listtag = tag_escape($listtag);
       	$itemtag = tag_escape($itemtag);
       	$captiontag = tag_escape($captiontag);
       	$columns = intval($columns);
       	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
   
       	foreach ( $attachments as $id => $attachment ) {
       		$link = wp_get_attachment_image($id, $size, true);
       		$output .= '<a href="'.wp_get_attachment_url($id).'">'.$link.'</a>';
       	}
   
       	$output .= "<br style='clear: both;' />";
   
       	return $output;
       }
       ```
   

The topic ‘clean gallery shortcode’ is closed to new replies.

## Tags

 * [gallery](https://wordpress.org/support/topic-tag/gallery/)
 * [gallery shortcode](https://wordpress.org/support/topic-tag/gallery-shortcode/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 0 replies
 * 1 participant
 * Last reply from: [Edouard Duplessis](https://wordpress.org/support/users/eduplessis/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/clean-gallery-shortcode/)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
