Flickr Group Widget – Round 1


flickr photo shared by Little Orange Crow under a Creative Commons ( BY-NC ) license

The goal here was simply to take the Flickr API knowledge I’d gained earlier and apply it within a WordPress widget.

In doing so, I learned a few things. The primary one being that I often harm myself by being fairly good at making stuff work rather than understanding what I’m doing. That’s what I did here. The Flickr API thing was pretty legitimate. I knew what I was doing there but I pretty much crammed it into a widget plugin template without really understanding the whole thing. That led to more confusion around print << than I’d like to admit before I broke down and googled it. It is kind of an interesting option of which I’d been totally unaware.

I did get it working but it doesn’t have any options or anything at the moment. I hamstrung myself by trying to skip a bunch of steps. I looked for a widget template and found this one. I should have gone to the WordPress codex widgets reference and made the thing from scratch but I was cutting corners and was impatient. In speeding up some things I hampered both my purpose (learn how to make widgets) and was stuck with some choices I didn’t quite understand.

In any case, round one is here (and commented up) and should function if you replace YOURAPIKEYHERE with your API key from Flickr. I plan to rewrite it from scratch and make it so you can enter your own API key, group ID, number of photos etc.

‘vcuphotowalk’,
‘description’ => ‘Recent photos from VCU photowalk group’
);

$this->WP_Widget(
‘vcuphotowalk’,
‘VCU Photowalk Recent Photos’,
$widget_ops
);
}

function widget($args, $instance) { // widget sidebar output
extract($args, EXTR_SKIP);
echo $before_widget; // pre-widget code from theme
//this the flickr specific stuff
$url = ‘https://api.flickr.com/services/rest/?method=flickr.groups.pools.getPhotos&api_key=YOURAPIKEYHERE&group_id=2799152@N22&per_page=14&format=json&nojsoncallback=1’;
$json = file_get_contents($url);
$obj = json_decode($json);
$flickrhtml = ”;

$list = array();
foreach ($obj->photos->photo as $media) {
$farmid = $media->farm;
$serverid = $media->server;
$photoid = $media->id;
$photosecret = $media->secret;
$photoid = $media->id;
$ownername = $media->owner;

$pageurl = ‘https://flickr.com/photos/’ . $ownername . ‘/’ . $photoid;
$image = ‘https://farm’ . $farmid . ‘.staticflickr.com/’ . $serverid . ‘/’ . $photoid . ‘_’ . $photosecret . ‘_s.jpg’ ;
$flickrhtml .= ‘

‘ ;
}

print <<