Try Tuts+ Premium, Get Cash Back!
Quick Tip: Integrating Colorbox Into the Native [gallery] Shortcode

Quick Tip: Integrating Colorbox Into the Native [gallery] Shortcode

The native [gallery] shortcode is nice, but not great. In this quick tip, we’re going to beautify it with jQuery.

The [gallery] shortcode is not great. You can’t disable its default CSS, you can’t add or edit the CSS classes, you can’t edit the default attributes (which is actually a general shortcode problem)… Although this stuff doesn’t bother the majority of WordPress developers, weirdos like me could complain! :)

In this quick tip, we’re going to get rid of the image attachment pages and let our sites’ visitors navigate through the images within a jQuery modal box.


One of the Best jQuery Lightbox Plugins Available: ColorBox

With less than 5KB (gzipped) and a wide range of browser support (which even includes IE6), Colorbox is my favourite jQuery lightbox plugin.

As you can see from the plugin’s page, it has loads of settings, methods and hooks so you can customize it any way you want. It also has 5 neat CSS-based themes.

Download the pack and extract colorbox.min.js and one of the 5 themes (the colorbox.css file and the “images” folder) into a folder called “colorbox” and upload that folder to your WordPress theme. Add the following code at the end of the colorbox.min.js file before uploading:

jQuery(document).ready(function($) {
	$(".gallery-icon a").colorbox({rel:"gallery"});
});

Shortcode-Ception: Building a Shortcode Which Uses Another Shortcode

I know that this is going to be a little bit weird, but it seems to be the cleanest way. We’re going to create the [jgallery] shortcode.

Tip Within the Quick Tip: If you plan to change all the gallery shortcodes in your posts after creating the [jgallery] shortcode, I recommend using the Search Regex plugin to search/replace [gallery] with [jgallery].

Like always, we’ll begin with creating the base shortcode function:

function jgallery_sc() {
	// No parameters? This is madness!
}
add_shortcode('jgallery','jgallery_sc');

Next, we will enqueue the CSS and the JS files. Don’t forget, jQuery will be enqueued automatically (if it’s not already), by specifying that the Colorbox script depends on it.

function jgallery_sc() {
	// Enqueue colorbox.min.js (and jQuery if it is not already loaded)
	wp_enqueue_script('colorbox-js', get_template_directory_uri().'/colorbox/colorbox.min.js',array('jquery'));
	// Enqueue colorbox.css
	wp_enqueue_style('colorbox-css', get_template_directory_uri().'/colorbox/colorbox.css');
}
add_shortcode('jgallery','jgallery_sc');

Everything is ready, except we need to use the [gallery] shortcode inside this function. We will use the do_shortcode() function and return the [gallery link="file"] shortcode:

function jgallery_sc() {
	wp_enqueue_script('colorbox-js', get_template_directory_uri().'/colorbox/colorbox.min.js',array('jquery'));
	wp_enqueue_style('colorbox-css', get_template_directory_uri().'/colorbox/colorbox.css');
	return do_shortcode('[gallery link="file"]');
}
add_shortcode('jgallery','jgallery_sc');

All done! After adding this function into the functions.php file of your theme, you can start using the [jgallery] shortcode right away. Let us know what you think in the comments below. Enjoy! :)

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Pingback: Quick Tip: Integrating Colorbox Into the Native [gallery] Shortcode | Qtiva

  • http://fatihtoprak.com/ Fatih Toprak

    i think great way to using lightbox pluging for attachments. Thanks @Barış. Also I ‘ll prefer yours than to mine in my next project (: Because the script is so minimal and compressed . Thanks a lot.

  • Jonathan

    I just don’t understand how you’re using the native [gallery] if your using [jgallery] … And why are you using empty do_shortcode(”) ?

    • http://wp.envato.com/ Japh Thomson
      Staff

      Hey Jonathan, thanks for pointing that out! I forgot to encode the [ and ]. All fixed now.

    • http://beyn.org/ Barış Ünver
      Author

      Yeah, the empty do_shortcode() function broke the meaning “using the native &91;gallery 93; shortcode” :). Thanks Japh! It seems that it was my mistake, sorry about that.

  • http://godsofart.com S3bY

    Interesting and useful tutorial! I always hated the attachment page!

  • http://magpielab.com Yoosuf

    to extend the Native WordPress gallery a step further i was using this code

    https://gist.github.com/2002547

  • http://www.leachcreative.com Andrew

    Wow, somehow I thought it would be a lot more complicated than that.

  • http://unkardinal.com/ shaun kardinal

    why not something more general and across-the-board, which grabs any link to an image file? this is my go-to colorbox bit:

    $(‘a[href$=".jpg"],a[href$=".jpeg"],a[href$=".png"],a[href$=".gif"]‘).colorbox({
    transition:’none’,
    width:’90%’,
    height:’90%’,
    rel:’gallery’,
    opacity:.25,
    fixed:’true’
    });

    • http://flashsolver.com Karim Abdelwahab

      I like this solution :)

  • http://www.amazing-web-design.co.uk/ Joe Elliott

    Hi Baris

    I had actually used a plugin for colorbox and the galleries, it was good but because it worked we tend to leave out the small problems. Think might use this to create a better plugin,

    Thanks
    Joe

  • http://maorchasen.com Maor Chasen

    That’s a bad practice editing the source file, as you mentioned early in the tutorial: Add the following code at the end of the colorbox.min.js file before uploading:
    If Colorbox’s core will be updates one day, the changes will be lost once you replace the files.

    • http://beyn.org/ Barış Ünver
      Author

      You’re right, I guess I got myself carried away with optimization! :) I just didn’t want to make people to load another JS file (say, colorbox.load.js).

  • http://kriix.com Kristijan

    Wow, it’s really small, this will be a great quick way to enhance client themes galleries :) Thanks

  • D. Munter

    Hi Baris,

    this is great! Do you have any idea how I can use the attributes of the normal [gallery] with [jgallery]? I’d, for example, like to do [jgallery columns="5"], but that doesn’t work. Thanks in advance!

  • Chris Raymond

    I am using a paid theme, Gridnik, which already loads colorbox from its theme includes/js directory. Given that, how would I go about using your code?

    Meaning that jquery and colorbox are already loaded in my theme, so how do I just need to add
    function jgallery_sc() {
    return do_shortcode(‘[gallery link="file"]‘);
    }

    add_shortcode(‘jgallery’,'jgallery_sc’);
    to my theme’s functions.php?
    Thanks.