Kirby

Blog (RSS)

News about Kirby and stuff

Follow kirby on Twitter
@getkirby for more
updates.

Tags

Flickrbadge

I was asked by Luke Dorny on Twitter yesterday about a good Flash-less option to show your latest Flickr pictures on your site. I knew from all the API fiddling for Zootool that the Flickr API can be quite annoying. You often need to make much more calls to get the stuff you want, than you would need with any API of comparable services.

I found a nice PHP class by Dan Coulter, which can handle pretty much any features, which the Flickr API offers. So I decided to build a plugin for it, because I guess that this is a feature, which quite a few of you can use for your own sites.

The result is still beta, but works nice so far. You can download the plugin from Github and I want to show you how to use it here.

Demo

Check out the demo first. I created some fast and crappy CSS for it. You can probably do much better, so don't judge me for the poor design :)

Installation

Get the flickrbadge.php from Github and paste it in your site/plugins directory. If the directory isn't there yet, just create it.

All PHP files in the plugins directory will be loaded by Kirby automatically and you can use them without further installation.

Flickr API Key

To get access to the Flickr API, you will need a Flickr API key. You can get such a key here: http://www.flickr.com/services/api/key.gne It takes only a few steps to create one.

Using the flickrbadge in your templates

Let's assume you want to display your latest pictures from Flickr on the homepage, so create a home.php file in your site/templates directory and open that in your favorite editor.

Add and adjust the following lines to get the badge running:

<?php 

$pictures = flickrbadge(array(
  'key'      => 'your-flickr-api-key',
  'username' => 'your-flickr-username',
  'limit'    => 4,
  'format'   => 'medium'
));
   
?>
<ul>
  <?php foreach($pictures as $picture): ?>
  <li>
    <a href="<?php echo $picture->url() ?>"><img src="<?php echo $picture->src() ?>" alt="<?php echo $picture->title() ?>" /></a>
    <h3><?php echo $picture->title() ?></h3>
    <div class="date"><?php echo date('d.m.Y - H:i', $picture->taken()) ?></div>
    <div class="views">Views: <?php echo $picture->views() ?></div>
    <div class="comments">Comments: <?php echo $picture->comments() ?></div>
    <div class="tags">Tags: <?php echo implode(',', $picture->tags()) ?></div>
  </li>    
  <?php endforeach ?>
</ul>

Of course you need to put in your own API Key and username. You are also free to adjust the HTML for the list of images. The plugin offers an array of options and variables you can use to customize the result just as you need it.

Caching

As I mentioned before, the Flickr API needs a lot of calls to get even basic information about you and your latest pictures, so when you open the homepage for the first time after installing the plugin, you will find it loading quite long.

I've built in a way to cache the results, so it won't call the API on each pageview, but you must take some steps to activate it.

Go to site/config/config.php and find the line where caching is disabled and enable it:

c::set('cache', true);

Make sure the cache directory is writable:

site/cache

Change its permissions to 0755. You can use the command line or your FTP-Client to do that. Afterwards reload the homepage and see if the plugin is caching its calls to the Flickr API. You will find a new flickrbadge directory inside the cache folder, where all the cache files are stored.

By default the plugin will keep the latest result for an hour before it will fetch new data from Flickr. You can change that in the options array:

<?php 

$pictures = flickrbadge(array(
  'key'      => 'your-flickr-api-key',
  'username' => 'your-flickr-username',
  'limit'    => 4,
  'format'   => 'medium',
  'refresh'  => 60*60*5 // cache for 5 hours
));
   
?>

Fork it!

As with all plugins and snippets in the Kirby Extension Repo, feel free to fork the code and build your own versions or improve mine. I build all those plugins to work mostly out of the box, but they are also intended as a starting point for your own ideas.

If you got feedback, let me know in the comments or send me an email: bastian@getkirby.com

‹ Back

blog comments powered by Disqus