---
title: Add rel Attribute to Image Links in WordPress Galleries
date: 2011-02-26T17:06:55+00:00
modified: 2011-02-26T17:15:53+00:00
permalink: https://kaspars.net/blog/add-rel-attribute-to-image-links-in-wordpress-galleries
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
category:
  - WordPress
---

# Add rel Attribute to Image Links in WordPress Galleries

If you want to use either [Fancybox](http://fancybox.net/) or [Lightbox](http://www.huddletogether.com/projects/lightbox2/) scripts for image galleries, you need to add the `rel` attribute to all full size image links. Here is how to do it:

```
add_filter('wp_get_attachment_link', 'add_gallery_id_rel');
function add_gallery_id_rel($link) {
	global $post;
	return str_replace('<a href', '<a rel="gallery-'. $post->ID .'" href', $link);
}
```

We simply replace `<a href` with `<a rel="gallery-n" href` where `n` is a unique post ID. This filter is applied in `<a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/post-template.php#L1147">/wp-includes/post-template.php</a>`.