HiFi CMS

Web Publishing made Less Complicated and More Powerful

A jQuery Flickr Feed Plugin

View the Demo | Download the Zip

We often work with clients that maintain accounts with Twitter, Flickr, Youtube and other services in addition to their website. Often they will want to pull in data from one of their accounts to their website.  With Flickr, this is pretty easy because they make a simple API available.  Having worked with it a few times, we decided to make it even easier to pull photos from a public feed.

flickr dogs

There are a few examples of this in use on the demo page.  All of the photos on this page have been used with the generous permission of John Roberts (@jroberts13).

Plugin Overview

This plugin works by pulling a JSON feed from Flickr and applying the data it gets back to a template.  For example, we can generate this list of pictures:

<ul>
	<li><img alt="Photo Title" src="http://farm4.static.flickr.com..." /></li>
	<li><img alt="Second Photo Title" src="http://farm4.static.flickr.com..." /></li>
</ul>

From the following jQuery:

$('ul').jflickrfeed({
	limit: 2,
	qstrings: {
		id: '44802888@N04'
	},
	itemTemplate: '<li><img alt="{{title}}" src="{{image_s}}" /></li>'
});

The plugin gets the feed from Flickr using AJAX and applies each image it gets back to the provided template.

The Plugin Options

There are a number of options available on the plugin, these are the defaults:

flickrbase: 'http://api.flickr.com/services/feeds/',
feedapi: 'photos_public.gne',
limit: 20,
qstrings: {
	lang: 'en-us',
	format: 'json',
	jsoncallback: '?'
},
cleanDescription: true,
useTemplate: true,
itemTemplate: '',
itemCallback: function(){}

Here is a description of each one:

  • flickrbase: This is unlikely to be needed.  It is used to set up the url the jQuery AJAX call will need to be made to.
  • feedapi: There are a number of feeds that flickr makes available.  The default is the public feed.  Here is the list of all that are available: http://www.flickr.com/services/feeds/. This has only been tested on feeds that return photos.  So, for example, don't expect good results using this plugin on the Forum discussion feeds.
  • limit: Set how many items you want to loop through.  Flickr seems to limit its feeds to 20, so that is the default.
  • qstrings: This is the most important setting. This is used to request the correct feed.  In my examples I use this to set the user id.  These are automatically added to the request url. Depending on which feed you use (http://www.flickr.com/services/feeds/) there are different sets of query parameters available: http://www.flickr.com/services/feeds/.
  • cleanDescription: Flickr puts all kinds of junk in the description it returns. By default this plugin is set to remove everything but the plain photo description.
  • useTemplate: Set this to false if you don't want to use the plugins templating system.
  • itemTemplate: The template rules are described below.
  • itemCallback: You can add a callback on each item.  The scope is set to the container and the item object is made available.

Typically, the only things that will need to be set are limit, qstrings.id and itemTemplate.

Using the Templates

In order to make it really easy to use any kind of markup needed with this plugin, a simple templating system has been build in.  Here is an example of a template:

<li>
	<a href="{{image_b}}"><img src="{{image_s}}" alt="{{title}}" /></a>
</li>

You can see that this is just basic html with a few special tags mixed in.  All of the tags are surrouned by double curly braces. The plugin works by putting in the correct information for each tag and each item into the template.

The tags that are available depend on what flickr returns for each item.  For the Public Photos API these are: title, link, date_taken, description, published, author, author_id, tags, and image.  For the image property, the plugin uses the Flickr URL scheme to make several sizes available.  You can read about this at http://www.flickr.com/services/api/misc.urls.html.  The image tags available are: image_s, image_t, image_m, image, image_b.

The Callback Parameter and Integration

The plugin's second parameter is a callback.  This has the scope of the containing element and has the entire data response available. This is a great feature if you want to integrate this plugin with others.  Let's say you want to integrate it with colorbox. If you call $('selector').colorbox() and then this plugin it won't work.  This is becuase the plugin is adding elements to the page after the colorbox call.  This is even true if you call colorbox after this plugin.  Since this uses ajax, your images won't be added to the page until well after most of your javascript has run.  (In computer time).

Instead, the trick is to use the callback function.  This allows you to pass in code that you want to run after the images have loaded.  So for example, you can do this:

$('#cbox').jflickrfeed({
	limit: 14,
	qstrings: {
		id: '37304598@N02'
	},
	itemTemplate: '...example...'
}, function(data) {
	$('#cbox a').colorbox();
});
			

Now the colorbox call won't be made on the photos until they are loaded.

Demo and Download

You can see a demo of this plugin on the demo page.  Additionally you can download a zip of this plugin and the example.

As always, if you have any questions or comments, leave them below.  Enjoy!

Eytan Buchman
Eytan Buchman

| Permalink

Thank you so much for this post. It was helpful, concise and saved me time. You make the internets happy.
Martin
Martin

| Permalink

Is this also useful for other websites like Youtube?
Joel Sutherland
Joel Sutherland

| Permalink

Eytan, Thanks! Martin, The structure probably is, but it is not compatible with youtube currently. I did some work on a site that needed a youtube feed pulled recently and I will release that code as soon as I get a chance to clean it up.
Kien
Kien

| Permalink

I've seen other posts on the flickr feed plug-in, but not as detailed as this one. thx for the post and including a demo!
Fred
Fred

| Permalink

Could you use this with tags - for instance, photos tagged to 'New York' or something?
Joel Sutherland
Joel Sutherland

| Permalink

Fred, This does work for tagged photos. You can use the qstrings variable to pass in tags. Flickr documents that here: http://www.flickr.com/services/feeds/docs/photos_public/
Peter Vidani
Peter Vidani

| Permalink

Thanks Joel! This was really easy to set up. I'm using it for a Tumblr theme, and it's initiated when the theme user enters in their Flickr ID. Is there anyway to make this compatible using their Flickr username instead?
Joel Sutherland
Joel Sutherland

| Permalink

Peter, I believe that requires using the Flickr API which I did not address in this plugin. Do you have a link to the theme? It sounds cool.
Jeff
Jeff

| Permalink

Great work! I have been looking for something like this to replace the standard Flickr badge.
Andreas
Andreas

| Permalink

Hi Joel, Thank you for a great tutorial! I now have multiple, custom feeds coming into my page. Feeling good about that. However, I could not get the colorbox function to work on more than one of the feeds at a time. (nationbranding.ambwork.com). Is that how colorbox works, or is it just my limited coding ability?
Joel Sutherland
Joel Sutherland

| Permalink

Andreas, That site is looking awesome! Great use of the plugin. I took a look at your code, rather than calling $("#cbox a").colorbox() on each, you need to use your selector. For example, $("denmark a").colorbox();
ziggy hil
ziggy hil

| Permalink

hey man this is AWESOME..exactly what i have been looking for. The only problem is my knowledge for doing anything web based is very limited ..as you can see - http://www.funns.com ive been having a play and honestly...i have no idea is it simple enough to explain to a mister no nothing like me? without wasting too much of your time if so then that would be amazing! im wanting this flickr http://www.flickr.com/photos/funns in the paint section of my website thank you ziggy
ziggy hil
ziggy hil

| Permalink

this bit is pretty much it http://www.funns.com/blog2.html but what bit do i use to put it inside my site?
Andreas
Andreas

| Permalink

Hi Joel, Fantastic! That is working better than I could have hoped for. Keep up the good work!
Alberto Contreras
Alberto Contreras

| Permalink

Joel, first: thx you very much for this excellent piece of code! second: the prob! (hehe) I'm working on my personal photograph porfolio and since I have pretty big images (1024x685) I need them to get pulled from Flickr and displayed on my site. There is a way to tell jQuerry Flickr to pull the original images from there? As far as I know Flickr keep a copy of the original one and it's called "_o" but can't figure out how to call it from your plugin. Any help will be deeply appreciated. Best regards! Cheers! And thanks!
Joel Sutherland
Joel Sutherland

| Permalink

Alberto, Flickr only allows the original photo to be pulled if you are using the API which requires a key. You would probably need to tweak the request in the plugin code to also authenticate with an API key. (This can MAYBE be done through the qstrings): http://www.flickr.com/services/api/misc.userauth.html You can read about the original sized format here and how there is more involved: http://www.flickr.com/services/api/misc.urls.html If you figure it out -- be sure to post a comment on what you changed. I will see if I can add it into the plugin or instructions.
Alberto Contreras
Alberto Contreras

| Permalink

Oh, thx for your quick response!. Gonna give it a shot to the API key through qstrings. I'm not a "advanced jquerier" by any chance but I'll let you know any progress I can come up with. Best regards,and again, thank you very much for your work and patience.
Leslie
Leslie

| Permalink

Is there a special way to add 'tags' into the qstrings? Sample: qstrings: { id: '48934342@N07' tag: 'BS2003' }, Am I missing anything?
Joel Sutherland
Joel Sutherland

| Permalink

Leslie, Yes there is. You need to use 'tags' instead of 'tag'. Here is the page on Flickr with the documentation for this: http://www.flickr.com/services/feeds/docs/photos_public/ There is also tagmode if you want to use more than one tag.
Lonnie Minter
Lonnie Minter

| Permalink

Am I able to pull from sets or does it only pull from the master photo feed? Thanks.
Elizabeth
Elizabeth

| Permalink

Thank you, thank you, thank you. This is exactly what I was looking for today!
Jason Day
Jason Day

| Permalink

Hi - great tutorial. What I'm looking for, that I can't seem to make work, is to pull a single image from flickr as a thumbnail that then opens the large version in a modal window (such as your colorbox example). Thanks
Rob
Rob

| Permalink

how can i pull from different sets? there is no option for sets here: http://www.flickr.com/services/feeds/docs/photos_public/
Joel Sutherland
Joel Sutherland

| Permalink

Rob, You can actually pull from any of the feeds: http://www.flickr.com/services/feeds/ Unfortunately it doesn't look like sets are available as a feed. You'll probably need to go through the Flickr API route if you need sets.
Brian McCulloh
Brian McCulloh

| Permalink

This line will not validate in an html validator: itemTemplate: '<li><a rel="colorbox" href="{{image}}" title="{{title}}"><img src="{{image_s}}" alt="{{title}}" /></a></li>' How can I get this to validate? The error is "document type does not allow element "li" here". Regards, Brian
Brian
Brian

| Permalink

Figures - messed with this for an hour, couldn't figure it out, posted my comment, then figured it out on my own 5 minutes later. The fix is to "comment out" the javascript using "<!--" and "// -->" so the validator doesn't see it. Fundamental oversight - my mistake. Great plugin - thanks! Brian
Kyle Mark
Kyle Mark

| Permalink

This looks excellent and I'd like to try it. One question & I'm new to this so please forgive me if it's common knowledge... Is there a way to grab flickr photos using a specific tag from a specific account? Example: 20 images in flickr, 8 tagged 'portfolio', can I specify load just the ones labeled 'portfolio'? If so, can someone point me to an example or explain? Thanks in advance.
Rob Che
Rob Che

| Permalink

Great code. I've been looking for ages for a lightweight codebase - ad this fits the bill perfectly. THANKS for sharing. Rob
Rob Che
Rob Che

| Permalink

Does anyone know a way to pull the thumbnails a different size? ie: bigger! Cheers Rob
Tiago Vieira
Tiago Vieira

| Permalink

Hi Just a minor contribution: ADDING GROUP PHOTOS If you want to add group photos, you need to: 1- Get the group ID: http://get-flickr-id.ubuntu4life.com/ 2- Open jflickrfeed.min.js 3- find 'photos_public.gne' and replace with 'groups_pool.gne' 4- Open setup.js and replace your ID That's it!
lyn beeran
lyn beeran

| Permalink

this is very great. i have needed to install multiple flickr feeds on my site. but i am wondering before i spend hours combing the internerd if you could tell me if it's possible with your system to include a feed of favourites from a particular user?
lyn beercan
lyn beercan

| Permalink

oops incomprehenible -try again <br> <br> --i copied jflickrfeed.js to jflickrfeed2.js <br> <br> --in jflickrfeed2.js on line 10 i added a '2' $.fn.jflickrfeed = function(settings, callback) { $.fn.jflickrfeed2 = function(settings, callback) { <br> <br> --on line 13 i changed: feedapi: 'photos_public.gne', to: feedapi: 'photos_faves.gne', (from http://www.flickr.com/services/feeds) <br> <br> --then in this section in setup.js i just added the '2' there: $('#exex').jflickrfeed2({ limit: 44, qstrings: { id: '51341535@N05' }, itemTemplate: '<li>' '<a rel="colorbox" href="{{image}}" title="{{title}}">' '<img src="{{image_s}}" alt="{{title}}" />' '</a>' '</li>' }, function(data) { $('#exex a').colorbox(); }); <br> <br> --i'm pretty sure that's it!
lyn beercan
lyn beercan

| Permalink

sorry, it just stays garbled. ----------but what i wonder now is how to add an api key, i assume this is the way to override flickr's 20 pic limit.
felipus
felipus

| Permalink

Hi, this plugin is just amazing. Can it be used with a specfic set of Flickr (each set has a unique feed)? If so how? (I'm not fluent with js). Thx!

Leave a Comment

URLs will be converted to links. HTML tags will be converted to entities.