Flickr for Advanced Custom Fields
Why use Flickr field?
How to set it up
First of, this isn't gonna work if you haven't downloaded and enabled Advanced Custom Fields (ACF) first. You also need a Flickr account to create sets and/or galleries. If you haven't got one yet, create one here (you can use your Google or Facebook account).
Now that you have ACF and a Flickr account you're ready to download my Flickr Field if you haven't already. Create a folder called fields in wp-content/themes/[YOUR THEME] and extract the flickr folder in there.
Make sure you do this right, or the field won't work.
The .ZIP contains the following files:
- flickr.php - This contains the Flickr field add-on for Advanced Custom Fields
- phpFlickr.php - phpFlickr is a class written by Dan Coulter in PHP to act as a wrapper for Flickr's API. In short, we use this class to retrieve data from Flickr.
- cache - This folder will be filled with phpFlicker's caching
You have to register the field with register_field($name, $path) in order to use it. The $path is the direct path to the fields directory in your theme. Put the code below in your functions.php
if(function_exists('register_field')) {
register_field('Flickr_field', dirname(__File__) . '/fields/flickr/flickr.php');
}
Remember that you can include any user IDs you want (aslong as they have public sets available). You just need an Flickr API key to make calls to the Flickr API.
OPTIONAL: If you're going to use this field several times in your website, you can use add_option to create an option for the flickr_api_key. If this exist it will be filled in automatically when making a new Flickr field. Copy the code below and replace [YOUR API KEY] with your flickr api key. Apply for a key.
add_option( 'flickr_api_key', '[YOUR API KEY]' );
How to use it
Go to Advanced Custom Fields in the menu and create a new Field Group. Use Add field to add a new field. Select Flickr field on Field type. Fill in your user ID and api Key like the screenshot below (click on the image for enlargement). Use the ACF Rules to add the field group to a post (obviously you can also select a page or custom post-type of your choice here).
Now that you've added the field you can go ahead and edit an existing post or add a new one and select a flickr set by clicking on the row with the desired set. You can deselect an active set by clicking on it.
Congrats, you added a flickr set to your post. Now you probably want to display the Flickr data in your template. Use the following code to display the flickr set (or gallery) images:
// Get the Flickr data by using get_field
$flickr_set = get_field('flickr_set');
// Check if an set or gallery ID exists
if (isset($flickr_set['id'])) {
// Require phpFlickr
require_once(dirname(__FILE__) . '/fields/flickr/phpFlickr.php');
$f = new phpFlickr($flickr_set['api_key']);
// Enable phpFlickr caching
$f->enableCache("fs", dirname(__FILE__) . '/fields/flickr/cache');
// Get all data based on Flickr ID (set or gallery)
switch ($flickr_set['flickr_content']) {
case 'sets':
$photos = $f->photosets_getPhotos($flickr_set['id']);
foreach ($photos['photoset']['photo'] as $photo) {
echo '
';
}
break;
case 'galleries':
$photos = $f->galleries_getPhotos($flickr_set['id']);
foreach ($photos['photos']['photo'] as $photo) {
echo '
';
}
break;
}
}
Use the Flickr API documentation and scroll the phpFlickr class to look for different functions and/or parameters.
To-do list
- Multi-select sets
- Handle more then 50 sets
- Tags, favorites and more

