WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add rel="xyz" to Gallery Link (15 posts)

  1. Tom
    Member
    Posted 4 years ago #

    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...

  2. Tom
    Member
    Posted 4 years ago #

    Anyone? Please?

  3. Tom
    Member
    Posted 4 years ago #

    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.

  4. Ahni
    Member
    Posted 4 years ago #

    Thanks a bunch, TM3909. Worked perfectly.

  5. sacornwell
    Member
    Posted 4 years ago #

    Beautiful, Thanks TM3909

  6. Casemon
    Member
    Posted 4 years ago #

    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.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    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..

  8. Tom
    Member
    Posted 4 years ago #

    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?

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    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... :)

  10. Tom
    Member
    Posted 4 years ago #

    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!

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  12. Tom
    Member
    Posted 4 years ago #

    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?

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.
    http://wordpress.org/extend/plugins/search-and-replace/

  14. Brig01
    Member
    Posted 4 years ago #

    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!

    Thanks!!

  15. Brig01
    Member
    Posted 4 years ago #

    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;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic