WordPress gallery add rel lightbox

Home / Wordpress / WordPress gallery add rel lightbox

Everyone to like lightbox or prettyphoto to show images in nicely in popup. In wordpress are many plugins to set popup for your images but need to insert or set rel to every images or gallery. But if you added all the images then you can decide to open image in popup that time very difficult to rework to set or edit rel in every images or gallery.

If you want to set automatically rel in every images or add all gallery images rel=lightbox or something like that then this post is very useful to you and save lot of time.

If you want to set all images url rel=lightbox or rel=prettyphoto then add this below function to your theme function.php file.

<!--?php <br ?-->// Custom functions
add_filter('the_content', 'my_addlightboxrel');
function my_addlightboxrel($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 rel="prettyPhoto[gallery1]

" title="'.$post->post_title.'"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;

if you display set all images using gallery or you can add rel to all images then copy this below function and put it on your theme function.php file.

<!--?php <br ?-->// Custom functions
add_filter('wp_get_attachment_link', 'add_gallery_id_rel');
function add_gallery_id_rel($link) {
	global $post;
	return str_replace('<a class="wpGallery mceItem" title="gallery1" rel="prettyPhoto<img src=">" href', $link); } ?>

If you want to add rel automatically using jQuery then add this below code in your footer.php file.

Add REL USING JQuery WordPress

jQuery('.gallery').each( function(g) {
    jQuery('a', this).attr('rel', function(i, val) { return 'prettyPhoto[gallery1]

' /*val + '-' + g*/; });

Hope this post is useful to many developer which automatically add rel attribute to WordPress images.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *