Title: Edit Gallery Shortcode
Last modified: August 20, 2016

---

# Edit Gallery Shortcode

 *  Resolved [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/)
 * Hello!
 * Where can I edit the code that the default wordpress gallery shortcode outputs?
 * `[gallery link="file" columns="4"]`
 * I’m manually installing “fancyboxy” and I want to add the class & rel tags to
   the html output of the shortcode.
 * Thanks!
 * -foo

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128673)
 * the code is in /wp-includes/media.php (from line 747);
 * in functions.php of your theme, add this:
 *     ```
       remove_shortcode('gallery', 'gallery_shortcode');
       add_shortcode('gallery', 'gallery_shortcode_fancybox');
   
       function gallery_shortcode_fancybox($attr) {
       	global $post, $wp_locale;
   
       	static $instance = 0;
       .....
       copy all the code from media.php until line 876
       .....
       			<br style='clear: both;' />
       		</div>\n";
   
       	return $output;
       }
       ```
   
 * then make your edits there; this way you avoid hacking core files.
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128692)
 * alchymyth,
 * Thanks, that helps, but I’m not sure where I add my html code `class="fancy" 
   rel="group1"` – is it somewhere within the $link variable?
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128705)
 * I should probably clarify that I’m simply trying to inject the html code into
   the img src for each thumbnail that gets generated in my galleries.
 * So it will look something like when I’m done:
 * `<img width="93" height="93" src="http://localhost/global/wp-content/uploads/
   2011/06/01-93x93.jpg" class="attachment-thumbnail fancy" rel="group1" alt="img-
   01" title="img-01">`
 * Thanks!
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128814)
 * > is it somewhere within the $link variable
 * yes – which makes it more complicated to acceess.
 * in the newly added code, before these lines:
 *     ```
       $i = 0;
       	foreach ( $attachments as $id => $attachment ) {
       ```
   
 * add:
 *     ```
       add_filter('wp_get_attachment_link', 'add_rel_to_gallery');
       ```
   
 * then, after these lines:
 *     ```
       $output .= '<br style="clear: both" />';
       	}
       ```
   
 * add:
 *     ```
       remove_filter('wp_get_attachment_link', 'add_rel_to_gallery');
       ```
   
 * finally, add this to functions.php of your theme, before the code you added earlier:
 *     ```
       function add_rel_to_gallery($link) {
       $link = str_replace('" alt="', ' fancy" rel="group1" alt="', $link);
       return $link;
       }
       ```
   
 * (untested)
 * everything for functions.php in one bit: [http://pastebin.com/yH724Myb](http://pastebin.com/yH724Myb)
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128844)
 * alchymyth,
 * That code works, however I just realized I need to place that styles on the a
   tag of the image, like so:
 * `<a href="http://localhost/global/wp-content/uploads/2011/06/brakingthecycle-
   01.jpg" title="brakingthecycle-01" class="fancy" rel="group1">`
 * How could I modify $link to do that?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128852)
 * as before, use the filter; i.e. change:
 *     ```
       function add_rel_to_gallery($link) {
       $link = str_replace('" alt="', ' fancy" rel="group1" alt="', $link);
       return $link;
       }
       ```
   
 * into:
 *     ```
       function add_rel_to_gallery($link) {
       $link = str_replace("'><img", "' class=\"fancy\" rel=\"group1\" ><img", $link);
       return $link;
       }
       ```
   
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128858)
 * Awesome. Thank you very much for your help alchymyth!
 *  [schoolspeak](https://wordpress.org/support/users/schoolspeak/)
 * (@schoolspeak)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128985)
 * I’m looking to do the same thing, but instead of wanting to add a class to “fancyboxy”
   I’m looking to add a class to “highslide”.
 * Can I just say:
    add_shortcode(‘gallery’, ‘gallery_shortcode_highslide’); ?
 * Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Edit Gallery Shortcode’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 3 participants
 * Last reply from: [schoolspeak](https://wordpress.org/support/users/schoolspeak/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/edit-gallery-shortcode/#post-2128985)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
