Tag Archives: jquery

Using the jQuery Lightbox Plugin in WordPress

Thickbox thickbox thickbox. Why on earth did Thickbox make it into the WordPress core and Lightbox didn’t? I’m pretty sure that most of you will agree with me – Lightbox is fancier than Thickbox, that cool effect on popup. And the way you can browse through the images in a gallery just by clicking the left and right parts of the image. Thickbox doesn’t have that! It took me a few seconds before I realized there were a “Next” and “Previous” links down there at the bottom of the image, next to the title. Jeez, it’s so impossible to find them! Especially when you click once, the next image is bigger, and bam! You lost the next and previous buttons again, cause they moved away from right under your cursor! That’s so irritating if you ask me. Anti-usability I’d say!

So, Lightbox, yeah okay. The hell with Thickbox being hard-coded into WordPress, let’s build our own themepark with Lightbox and jQuery. Note that we’re not going to use the WordPress Plugin called Lightbox, because that’s aweful (you should know if you’ve ever seen it). Let’s use the original jQuery Lightbox ;) – lightweight and smooth!

Lightbox originally works via Prototype and Scriptaculous (effects and builder) but personally, I don’t really like Prototype (any more) after I discovered jQuery, and god I was so happy when I found the jQuery Lightbox plugin! It’s a little bit more difficult to implement comparing to Thickbox, but believe me, it’s worthed! So let’s get started. Grab your jQuery lightBox plugin and extract the .min.js file, the .css and the images folder. That’s all you need to get it working with WordPress. Move the files over to where your theme is. I like to keep things organized, so I moved my css file into a new folder called css in my theme directory (themes/kovshenin/css), same goes to js (themes/kovshenin/js) and the images (themes/kovshenin/images/lightbox).

You’re half-way through. Open up your theme header.php file and add the following before wp_head() is called:

wp_enqueue_script("jquery");
wp_enqueue_script("jquery-lightbox", get_bloginfo("stylesheet_directory") . "/js/jquery.lightbox-0.5.min.js");
wp_enqueue_style("jquery-lightbox", get_bloginfo("stylesheet_directory") . "/css/jquery.lightbox-0.5.css");

You don’t have to enqueue jquery twice if you already did. Also, you can use combine the first two lines into one using dependencies check for wp_enqueue_script. Take a look at the Codex for wp_enqueue_script for more info.

The original (prototype) Lightbox usually reacts on rel=”lightbox” attribute on links. The jQuery version doesn’t react until you ask it to. Here’s what we gonna do – activate lightbox on all a.lightbox elements on the displayed page. That means all links that have the “lightbox” class. Here’s what you do in header.php AFTER wp_head() but BEFORE the closing head tag:

<script>
jQuery(document).ready(function(){
    jQuery("a.lightbox").lightBox({
        imageLoading: "<? bloginfo("stylesheet_directory"); ?>/images/lightbox/lightbox-btn-loading.gif",
        imageBtnClose: "<? bloginfo("stylesheet_directory"); ?>/images/lightbox/lightbox-btn-close.gif",
        imageBtnPrev: "<? bloginfo("stylesheet_directory"); ?>/images/lightbox/lightbox-btn-prev.gif",
        imageBtnNext: "<? bloginfo("stylesheet_directory"); ?>/images/lightbox/lightbox-btn-next.gif"
    });
});
</script>

Note that we also pass the image parameters to the lightBox() function and use the WordPress bloginfo() function to display our stylesheet directory. That’s the right way to do it. Now all you have to do is try it out, write a new post and insert an image, like this:

<a href="path/to/image.jpg" class="lightbox"><img src="path/to/thumbnail.jpg" /></a>

Done. You can also group images into galleries using the rel attribute:

<a href="path/to/image.jpg" class="lightbox" rel="lightbox[gallery1]">
    <img src="path/to/thumbnail.jpg" />
</a>
<a href="path/to/image2.jpg" class="lightbox" rel="lightbox[gallery1]">
    <img src="path/to/thumbnail2.jpg" />
</a>

And so on.

One more thing. The NextGen Gallery for WordPress can use effects to show your images, the default effect is of course Thickbox, but you can change that to Lightbox. Just remember that we’re applying the lightBox effect to link with the “lightbox” class, so you should manually edit the link Code line to look like this:

rel="lightbox[%GALLERY_NAME%]" class="lightbox"

All done. Congrats ;)



Extending the jQuery Lightbox Plugin: Custom Link

Lightbox. I love it, though one interesting limitation I came over today is that there’s no extra linking possibilities. Yeah, the back and forth links are cool, but I’d like to take my visitor to some place whenever he clicks the full-sized image, not just close the lightbox. A good example may be an art gallery, where you can view an image set via lightbox, and then suddenly you decide to view one of the images in more detail (comments, download link, ratings and other stuff that you don’t put in the title attribute to show below the image). Wouldn’t it be nice if the user was lead to that specific art piece page if they clicked in the center of the image?

I made a little hack for that. I worked with the jQuery version of Lightbox but this trick should be applicable to the original (prototype) version too. So, here’s the trick. This is the usual way:

<a class="lightbox" href="path/to/full_image.jpg" title="title of the image">
    <img src="path/to/thumbnail.jpg" alt="whatever" />
</a>

And this is my version:

<a class="lightbox" href="path/to/full_image.jpg" title="title of the image"
    rev="http://url/of/some/page.htm" >
    <img src="path/to/thumbnail.jpg" alt="whatever" />
</a>

So our rev attribute will hold the URL where we’d like to take our visitors if they clicked the center of the image. Now, open up your lightbox.css file and find the #lightbox-nav identifier, and add a cursor:pointer; property. This is so that people actually notice that the image is clickable (you get that finger cursor, just like on any link). Now, if you’re working with image sets (more than one image) you’ll have the next and previous links appear and they take 49% of the image each (with the default settings). Let’s give our visitors some more room and decrease that down to 15%. It’s located in the lightbox.css file, identified by #lightbox-nav-btnPrev, #lightbox-nav-btnNext (change width: 49%; to width: 15%;).

Finally, the javascript part. We need two modifications here, one that will read our link rev attribute and store it in an array, the second is the actual linking. Find the _start() function in lightbox.js. Mine is on line 66. Find this line:

settings.imageArray.push(new Array(objClicked.getAttribute('href'),
    objClicked.getAttribute('title')));

And change it to:

settings.imageArray.push(new Array(objClicked.getAttribute('href'),
    objClicked.getAttribute('title'), objClicked.getAttribute('rev')));

One more… Find:

settings.imageArray.push(new Array(jQueryMatchedObj[i].getAttribute('href'),
    jQueryMatchedObj[i].getAttribute('title')));

Swap with:

settings.imageArray.push(new Array(jQueryMatchedObj[i].getAttribute('href'),
    jQueryMatchedObj[i].getAttribute('title'), jQueryMatchedObj[i].getAttribute('rev')));

And the final part – we need to add an event for whenever the visitors click the image. We’re not binding the click event on the actual image though, instead we take the navigation container which holds the next and previous buttons (in case you’re using image sets) and bind it there, because it’s more visible to the browser (z-index is around 10). Okay so find the _set_interface() function in lightbox.js. Mine is on line 126. Somewhere after the $(‘body’).append part type in this code:

$("#lightbox-nav").click(function() {
	location.href = settings.imageArray[settings.activeImage][2];
});

The settings.imageArray is the one being filled at the _start point we changed previously. We added a third index to the array, therefore we use the [2] (zero-based, remember?). Okay, done. Go ahead and try it out. Hope it worked for you too ;)



Thickbox and jQuery in WordPress 2.8

The update was great! The new features in WordPress 2.8 are awesome! Most of my Twitter friends have updated to 2.8 and haven’t had a single issue. The new widgets area is so cool! Oh jeez, I’m so excited. For a full features list check out Version 2.8 section in the Codex.

Anyway, the only issue I had so far is the Thickbox usage. I’m not sure why it’s lost, but I’ve discovered it in the Quick Flickr Widget plugin when upgrading. The thickbox feature worked fine in 2.7 and 2.7.1. For an unknown reason, WordPress 2.8 doesn’t call the thickbox.js file through wp_enqueue_script. The other js libraries work fine though (prototype, scriptaculous). jQuery’s being called out too btw, cause it’s a dependency of Thickbox, but thickbox never shows up. This is actually weird. I hope I’m not the only one with this issue, cause it’s starting to feel bad ;)

I’ll submit a bug ticket to their Trac for 2.8.1, but until that here’s a temporary workaround:

global $wp_version;
if ($wp_version == "2.8") wp_enqueue_script("thickbox28", "/wp-includes/js/thickbox/thickbox.js", array("jquery"));
else wp_enqueue_script("thickbox");

Hope this helps. And don’t forget to inject the thickbox CSS and javascript settings in the wp_head action.

Update: Thickbox loads in the footer section in WordPress 2.8, that should be right before the closing body tag. And yes, loads of themes are lacking the wp_footer() function call. So, forget about the workaround, fix your themes first.

The 1.2.9 update of Quick Flickr Widget included the fix above, so 1.2.10 reverts that fix back to the standard way, sorry for the hassle ;)



Compatibility: Twitter Tools & Twitter Friendly Links

Due to many requests for twitter friendly links in the Twitter Tools plugin by Alex King I’ve decided to take a look. Alex King surely did a great job in his plugin by leaving a filter which I could use in my plugin without touching the sources of Twitter Tools.

The toughest thing was to find this great wordpress function called url_to_postid() which takes a wordpress URL and tries to convert it into a suitable post ID. It isn’t documented in the codex and I had no idea what keywords to use to find it through Google (was looking for permalink to post id, permalink to id. I’ve highlighted these so that other peeps could find it faster than I did).

Now I’ve got a new function available in my plugin:

echo permalink_to_twitter_link($permalink);

And of course I’ve added an option called “Twitter Tools fix” in the Settings page of the Twitter Friendly Links plugin to let you fellows decide whether you want cool short links or those nasty long ones. I ran a couple of tests on a testing Twitter account, seemed to work fine for me. Comments and suggestions are welcome as usual ;)

The fix was introduced in version 0.3.2. The AJAX/jQuery pagination was also fixed in this release. Available in the WordPress plugin directory.

Cheers!



jQuery in WordPress: wp_enqueue_script

Did anybody notice the new sitemap I did for the blog? Top-right, in the black part. Click on sitemap. Smooth! For those of you who don’t know, that’s jQuery. Okay, seriously, this is my first ever jQuery script because I love Prototype. I don’t know why I did that, since I could have done it with Scriptaculous. So, about scripts in WordPress… You all heard about the function wp_enqueue_script in WordPress, yeah it’s cool, but what if I want to use both Prototype AND jQuery? Yeah, sounds stupid, but what about having two different plugins installed. One that uses Prototype, the second one jQuery. Compatibility?

Took me quite some time to figure out how to use jQuery $() function in WordPress though. I really think that the man who came up with this is a genius. There’s a synonym for the $() function in jQuery called jQuery(). WordPress’ers must have removed the $() function for Prototype compatibility.

jQuery(document).ready(function(){
	jQuery(".sitemap-click").click(function(){
		jQuery("#sitemap").fadeIn("slow");
		return false;
	});
});

So instead of $() we use jQuery(). Sweet.

Oh and I’m also glad that Skype on iPhone will be free and official!