Archive for August, 2012

Yet Another WordPress Hack

I do not very well remember how much I’ve screwed around with WordPress and its plugins, but here is one of my latest mods to an existing plugin that I believe will be very useful for users wanting to import all photos from their photo albums of a particular Facebook page. I had been searching around for plugins, and could never find one that could actually do the job. Either if a plugin could display all photos from all albums it didn’t have the feature to read from a Facebook page and was rather limited to just Facebook users, like the Fotobook plugin. Or if it could read from pages, then I had to specify each and every album ID separately in the shortcode, and add as many shortcodes as there are albums in that Facebook page. And moreover, updates in the gallery page to include the new shortcodes for newly added albums on Facebook had to be done manually. It fit in only if I wanted to display selected albums. But then I had to just display all of them, and there were more than ten albums that could possibly keep increasing every month or sooner.

And fetching the album IDs of each of those ten albums was trouble again. Say you open an album and read the URL on the address bar. You would see something like this: https://www.facebook.com/media/set/?set=a.10151028573106217.485132.286244581216&type=3. The number in between the first dot after ‘a’ and in between second dot becomes your album ID. And would I like to manually read each of those album IDs, and that too every time a new album is added? And then manually add it to the page in the website? No!

So I found this plugin Facebook Page Photo Gallery, but which like I said could only display based on the album ID in specific, and it required me to add each album ID manually as a shortcode:

[fbphotos id=albumid]

I liked the overall feel of the plugin, and the thumbnail style and the jQuery effects that the developer had used in his plugin. And then it was time for me to start digging into the code. I narrowed down to this function, where in he generates a string containing the HTML to be printed, which again is generated by calling the fppg_get_photos() function by passing the album ID. Here is the original piece of code from facebook-page-photo-gallery.php, starting from line 34.

/*Function to hook to an embedding shortcode 
 * @param $album array passed through shortcode
 * 
 */

function fppg_embed_photos($album) {

static $count=0;
$count++;
extract( shortcode_atts( array(
		'id' => '',
		'limit' => 300,
                'rand'=>false
	), $album ) );
$div='
'.fppg_get_photos($id,$limit,$rand).'
'; return($div); }

Great. What I needed to change was to loop over the piece of code generating the HTML for each and every album rather than just the one album. So here is what I did. This was the page of my interest: https://www.facebook.com/pages/Sunshine-Foundation/286244581216, and its ID was 286244581216. And then I went to https://graph.facebook.com/286244581216/albums to get the details of all albums of that particular page in JSON format.

Just a small note here, if suppose a page has a registered username and does not have its ID in the page URL, say like https://www.facebook.com/CocaCola, then go to https://graph.facebook.com/CocaCola to retrieve its ID as one of the JSON objects.

Now that I had details of all the albums of a particular page in JSON format, the next thing that I needed to do was parse the JSON and retrieve the IDs of all the albums from the JSON, and run these album IDs in a loop through the previous code. The modified code is here, with the function fppg_embed_photos() replaced by mine.

function fppg_embed_photos($album) {

static $count=0;
$count++;
extract( shortcode_atts( array(
		'id' => '',
		'limit' => 300,
                'rand'=>false
	), $album ) );

/* Fetching the JSON Data from the graph.facebook.com/pageid/albums URL */
$fbAlbums=file_get_contents('http://graph.facebook.com/'.$id.'/albums');

/* Decoding the JSON Data and storing it in $fbData */
$fbData=json_decode($fbAlbums,true);

$div=" ";

/* Looping each $fbData['data'] */
foreach ($fbData['data'] as $fbPhotoAlbum)
{
/* Fetching 'id' of each album by $fbPhotoAlbum['id'], and passing that to fppg_get_photos(), and concatenating each of the generated HTML with itself and assigning it back to $div */
/* Also printing the album name by fetching it from the JSON data using $fbPhotoAlbum['name'] - Optional */
/* You can replace   as however you would like to, like for example 
  • for each album name to be in a bulleted list */ $div=$div.''.$fbPhotoAlbum['name'].'
    '.'
    '.fppg_get_photos($fbPhotoAlbum['id'],$limit,$rand).'
    '; /* Removing album names of any album with the name 'Untitled Album' - Optional */ $div=str_replace('Untitled Album;','',$div); } return($div); }
  • There we go, and this is how it looks on site, here. You can find the edited source code for download here. Download the original plugin, and replace the facebook-page-photo-gallery.php with the new downloaded file after extraction. And then now, your shortcode will have to be called only once, with the page ID of the Facebook page, rather than with some album ID.

    [fbphotos id=pageid]

    That does it! :). And don’t mind coming back to your photo gallery page ever again to update or add anything. The albums will automatically update as they are fetched from the Facebook page. Just keep those albums and photos rolling on in Facebook, and see them spring up there.

    Comment
    Copyright © Bits and Bytes | Arjun Abhynav. Powered by WordPress.