Skip to content

Instantly share code, notes, and snippets.

@leowebguy
Last active December 14, 2015 13:48
Show Gist options
  • Save leowebguy/69548345f33d9273737f to your computer and use it in GitHub Desktop.
Save leowebguy/69548345f33d9273737f to your computer and use it in GitHub Desktop.
Quick integrate lightbox to WordPress
<?php
/****
* Download lighbox js on http://lokeshdhakar.com/projects/lightbox/
* Upload to your server Ex. /public_html/lightbox/
*****/
function enqueue_lightbox() {
wp_register_style( 'lightboxcss', get_stylesheet_directory_uri() . '/lightbox/css/lightbox.css' );
wp_register_script('lightbox', get_stylesheet_directory_uri() . '/lightbox/js/lightbox.js','','',TRUE);
wp_enqueue_style( 'lightboxcss' );
wp_enqueue_script('lightbox');
}
add_action( 'wp_enqueue_scripts', 'enqueue_lightbox', 11);
function add_rel_attribute($link) {
return str_replace('<a href', '<a data-lightbox="lightbox-gallery" href', $link);
}
add_filter('wp_get_attachment_link', 'add_rel_attribute');
/* here is one 2nd option
function add_lightbox_rel( $content ) {
global $post;
$get_img ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$img_replace = '<a$1href=$2$3.$4$5 rel="lightbox['.$post->ID.']" title="'.$post->post_title.'"$6>';
$content = preg_replace($get_img, $img_replace, $content);
return $content;
}
add_filter('the_content', 'add_lightbox_rel'); */
/***
Use this to add title attribute copying it from alt attribute
****/
jQuery('figure a img').each(function(idx, element) {
jQuery(element).parent().attr('title',jQuery(element).attr('alt'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment