How to Add jQuery Effects to Your WordPress Blog Without Installing Plugins
March 4th, 2010 by
In my previous post I explained why it’s better to add functionality to your blog by customizing functions rather than installing plugins.
For the sake of example, I’ll present a method to add lightbox effect (FancyBox jQuery plugin to be exact) to post images without using a WordPress plugin.
The FancyBox for WordPress plugin does a good job in integrating the FancyBox jQuery plugin into your blog.
However, I don’t like the fact that the files fancybox.css and fancybox.js are loaded for each and every page in my blog, even ones that don’t have images in them!
Instead of installing a WordPress plugin, I recommend integrating the fancybox effect only for images you choose, by adding just a few lines of code to your theme files.
Add the following code to functions.php file (if you don’t have one in your theme folder, simply create the file and make sure it is stored in your theme folder).
function loadFancyBox()
{
if (is_single() && strpos($post->post_content,'class="fancy"') !== false)
{
wp_enqueue_style('fancyStyle', '/wp-content/themes/yourThemeName/fancyBox/jquery.fancybox-1.3.0.css');
wp_enqueue_script('fancyScript', '/wp-content/themes/yourThemeName/fancyBox/jquery.fancybox-1.3.0.js', array('jquery'), '', true );
}
}
add_action('wp_print_styles', 'loadFancyBox');
The code block above adds the ‘loadFancyBox’ function to be loaded along with the WordPress ‘wp_print_styles’ function. This code is inspired by the code in Joost de Valk’s post.
Lines 3-7 specify what shall occur if the following two conditions are met:
- The current page is a single post.
- The post being displayed, contains the string class=”fancy” inside its HTML.
Only if both conditions are met, the css and javascript files are loaded.
To apply the effect successfully in a post, make sure to add class=”fancy” like in the following example:
<a class="fancy" href="/wp-content/uploads/2010/03/large_image_for_fancy_effect.jpg">
<img src="/wp-content/uploads/2010/03/thumb_image.jpg">
</a>The last thing left to do is call the jquery function for those images.
To avoid creating a separate javascript file for this call, simply copy and paste the following lines to the end of jquery.fancybox-1.3.0.js file, just before closing })(jQuery);
So the end of the file will look like this:
$("a.fancy").fancybox({
'titleShow' : false,
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
});
})(jQuery);
You can then modify the transition effect and other properties according to the instructions here.
So you can see it’s really not that big of a deal to to get along without WordPress plugins for jQuery effects, even if you’re not a coder. Keeping your blog fast and clean will make your visitors happier and is definitely worth the few extra minutes of tweaking.

nice tutorial bro.. thank you
nice and very useful
gonna implement in my blog very soon
thank
@denbagus, @a – Thank you, I am glad you find this post useful.
This is just what I’ve been wanting to figure out how to do.
I do, however have a two-fold question: what if you want to have a group of images that show up in the fancybox, but you don’t want to have thumbnails of all of them in the post; would you need to include some sort of “group” name for the images, and would that be applied to a “rel” attribute inside the <a> tag?
and second, could you then just have something like this in the post:
<a href=’path-to-first image’ rel=’groupname’>see the work here</a>
<a href=’path-to-second image’ rel=’groupname’>see the work here</a>
<a href=’path-to-third image’ rel=’groupname’>see the work here</a>
this is what I’m thinking of trying, but thought I’d send out the query and see if anyone knows if I’m on the right track.
Thanks much!
Hi jmb, I modified your comment to display code correctly. To include HTML in your comments you can use this online converter before you paste the comment http://bit.ly/1a9E1E
Regarding you question – what you suggested is more or less how it should be done. If you don’t want to display all thumbnails, you will need to hide them via display:none CSS property.
In addition, take a look at how it is done at the plugin site:
1. Download the fancybox package from here http://fancybox.net/
2. View the source of the index.html file. lines 114-118 show you the HTML and lines 44-50 show the corresponding jquery code.
Tell me if it works for you
Hi Omer.
Thanks for your quick response. I’ve bookmarked the converter for future reference!
Try as I might I wasn’t able to get fancybox to work via altering my functions.php file. I understand conceptually what needs to happen, and I understand the code I needed, but For some reason the js file was simply not being read.
In the end, I opted to use thickbox since it is already included with WordPress- and does the job. I placed two lines of text in my header file to call up the js and css files, since I want the modal window to be available on the index.php as well as the single.php pages. At some point, I’m sure I can write a short loop to exclude other types of pages from loading the css and js for thickbox.
Thanks again, for your help! (In case you want to check it out you can see it at jmbroekman.com/news)
Nice tutorial. Been dabbling with WordPress and getting familiar with the whole environment. Very interesting stuff.
I presume that in order to complete the steps above, I also need to download the FancyBox code and add it to my theme folder too, correct?
It’s probably a dumb question but I just wanted to double-check my thought process on this.
Hi Jason. your question is to the point, no worries.
Yes, you need to download and copy the fancybox code to your theme folder.
Great tutorial! Are there changes that would need to be made for implementing this on WordPress pages instead of posts?
Hi Sara,
In case you want to use the above code for pages, you can replace the condition in line 3 from is_single() to is_page(). I hope this helps
Thats really slick, nice little tutorial. Some plugins just dont even make an effort to get near their clean coded bretheren!
Kimberly
Thanks Kimberly
Very nice! Been looking for a way of doing just this without a plugin. Thanks!
Thanks Jonny.
great tut, Omer – thanks!
Wondering where jquery itself is called…? (sorry, total WP noob)
:-/
Also, your thoughts on the “Use Google Libraries” plugin?
Thanks again, great site!
Thanks MSM and sorry for the very late response. jQuery is called in line 6 where it says “array(jquery)”. This parameter tells wordpress that the fancybox script depends on jquery and so it needs to load jquery along with it.
I think using Google libraries is a good solution if you want to lighten up your hosting server http requests.
Thanks very much! Will be using this soon =)
useful tutorial, thanks
Thank you Keri and yadirosadi.
Hi. Thanks for this snippet but I can’t make it work. I past theme in my functions.php file and nothing. Maybe you know why this code doesn’t work. All my scripts except this is loading from scripts.php. This function I try to use with prettyPhoto, not FancyBox. Instead class=”fancy” I’m using rel=”prettyPhoto”. What you think, how to resolve this? Thank you!
Hi Denis, you mentioned you are using scripts.php. Did this file came with your theme or did you create it?
No, I’m create this file.
nice and very useful
gonna implement in my blog very soon
thank
Hi, mate. Great post – very informative and useful! I was just wondering if you know what changes need to be made to make fancybox load on ALL post images (as opposed to only those with the “fancy” class)?
Thanks!
Hi,
you should change $(“a.fancy”) to something like $(“img”) or $(“a”) for links (see the last code box in this post).
Great tips here. I have a much more involved request though.
How can I add this functionality to a lot of existing posts like the plugin does when you install it?
Anything ideas?
Hi Derek,
As long as you have the ‘fancy’ class (or any other defined class for that matter) assigned to all links that contain the images in all posts, it should work everywhere.
I’m having an issue getting this to work. Am I supposed to be including any other files outside of jquery.fancybox-1.3.4.css and jquery.fancybox-1.3.4.js?
AJ, can you please give more details? Does it show the images without the effect or the effect doesnt work as expected?
If I would want to apply this to pages only instead of posts only would line 3 be…
if (is_page() && strpos($page->post_content,'class="fancy"') !== false)thanks
where the example of effect jQuery? i’m not found get it. but thanks for tutorial.
A great article…very helpful. Thanks
Great info.
However, I think you should also consider caching.
Your blog aill load only once the jQuery lib.
So you actually save a single load of ~60 msec.
Am I wrong?
wp_enqueue_script(‘fancybox’, get_stylesheet_directory_uri() . ‘/library/js/jquery.fancybox-1.3.0.js’);
more common in use