Support » Fixing WordPress » Add rel=”xyz” to Gallery Link

  • Resolved Tom Morton


    Alright. I’m looking to do site-wide lightboxing of all my galleries. I’ve got everything in place, and it all works, I just need to add a rel=”lightbox” to the Gallery Shortcode, but I can’t seem to get it to work!

    I figured it was a media.php hack, but I’ve looked across the internet and can’t find a solution. Please help…

Viewing 14 replies - 1 through 14 (of 14 total)
  • Anyone? Please?

    Figured it out!

    I changed line 946 in wp-includes/post-template.php to this:

    return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title' rel='lightbox[gal]'>$link_text</a>", $id, $size, $permalink, $icon, $text );

    If you need any more info message me or post here.

    Thanks a bunch, TM3909. Worked perfectly.

    Beautiful, Thanks TM3909

    Editing source files = teh suck 🙂

    There is a plugin that does the above and more: Cleaner Gallery

    Still would be useful to add rel links to gallerys, so that we can control slideshows.

    This bit right here..

    apply_filters( 'wp_get_attachment_link'

    Is an indication of a filter hook, you could have added a filter and avoided the need to hack a core file..

    t31os_ I’ll be honest, while I consider myself an advance user, what you posted seems Greek to me :). Could you explain further, give me an example of how to implement something like that?

    Sure, here you go..

    add_filter( 'wp_get_attachment_link' , 'add_lighbox_rel' );
    function add_lighbox_rel( $attachment_link ) {
    	if( strpos( $attachment_link , 'a href') != false && strpos( $attachment_link , 'img src') != false )
    		$attachment_link = str_replace( 'a href' , 'a rel="lightbox" href' , $attachment_link );
    	return $attachment_link;

    I used strpos, and str_replace in favour of a regular expression as it’ll be faster, and the change really is only minor…

    The 2 strpos checks should be sufficient in making sure the replacement on attachment links only does so when they contain images.. (i assume that’s what you’d want)..

    Hope that helps… 🙂

    Alright, I’m calling uncle. I’ve done several google searches and more! How the heck do you implement the code you posted? In a plugin? In functions.php? I’m going Mad!

    Theme functions.php will be fine, although you can use it as a plugin if you prefer.

    Maybe there is something wrong with what I am doing, but I copy/paste the code into the functions.php file and it doesn’t change the gallery images at all.

    Am I doing something wrong?

    It’s not going to fix the existing ones, that would require additional scripting..

    You could try the search and replace plugin for updating posts that were published prior to implementing the code above.

    Sorry to revive an old thread but this function is exactly what I need but I cannot get it to work.

    I’ve put the function in my functions.php

    add_filter( 'wp_get_attachment_link' , 'add_lighbox_rel' );
    function add_lighbox_rel( $attachment_link ) {
    	if( strpos( $attachment_link , 'a href') != false && strpos( $attachment_link , 'img src') != false )
    		$attachment_link = str_replace( 'a href' , 'a rel="lightbox" href' , $attachment_link );
    	return $attachment_link;

    When I insert an image it doesn’t add rel=”lightbox”. Do I have to call this function anywhere to get it to work?

    I really need to get this function to work, please help!


    Never mind, figured it out!

    add_filter('the_content', 'addshadowboxrel', 12);
    add_filter('get_comment_text', 'addshadowboxrel');
    function addshadowboxrel ($content)
    {   global $post;
    	$pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
        $replacement = '<a$1href=$2$3.$4$5 rel="shadowbox['.$post->ID.']"$6>$7</a>';
        $content = preg_replace($pattern, $replacement, $content);
        return $content;
Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Add rel=”xyz” to Gallery Link’ is closed to new replies.