WordPress Twitter Followers Widget

I was using a plugin form another developer that inextricably stopped working, not sure why or how. After contacting the author and getting zero response I decided to write my own twitter widget plugin for WordPress.

 All I need are [x] number of profile pictures to display in my template sidebar, below is my crude “version 1.0″ proof of concept, I intend to update as and when I have the time. For now it works as you can see on the sidebar of this page.  a text version is available here http://www.jasonwhite.co.uk/mytwitterfollowers.txt

Features to add:

  • Dynamic Twitter ScreenName
  • Dynamic Display Number
  • Cache, with expiry timeout in minutes or views

The Code:

<?php

/*

Plugin Name: myTwitterFollowers

Plugin URI: http://www.jasonwhite.co.uk/

Description: Adds Twitter Followesr to your sidebar

Author: Jason White

Version: 1.0

Author URI: http://www.jasonwhite.co.uk/

*/

$twittername = 'iJasonWhite';

$displayQTY = 60 ;

function myTwitterFollowers()

{

	//get twitter name

	$twittername = $GLOBALS['twittername'];

	//get number to display

	$displayQTY = $GLOBALS['displayQTY'];

	//twitter api url

	$API_URL = "http://api.twitter.com/1/statuses/followers/".$twittername.".json";

	//Initialize a cURL session

	$curl = curl_init(); 

	//Set an options for a cURL transfer

	//this line sets the url to fetch

	curl_setopt($curl, CURLOPT_URL, $API_URL);

	//tell curl to return output as a string

	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

	//get the output into our variable

	$curloutstring = curl_exec($curl);

	//close out curl object

	curl_close($curl);

	//take the twitter JSON encoded response and convert it into a PHP variable.

	$response = json_decode($curloutstring, true);

	//initialize our count variable used in the following loop

	$count = 0;

	//hardcoded inline styling is BAD, don't do it! :P 

	echo '<div style="padding-left:12px;">';

	//lets loop each item(follower) in the response

	foreach($response as $friends){

		//check to see we are displaying only the qauntity that we want

		if ( $count == $displayQTY ) 

		{ 

			//if we are, brak the loop

			break; 

		}

		//profile data variables

		$thumbnail = $friends['profile_image_url'];

		$screenurl = $friends['screen_name'];

		$screenname = $friends['name'];

		//print out profile photo and link to twitter profile

		echo '<a target="_blank" title="'.$screenname.'" href="http://www.twitter.com/'.$screenurl.'"><img hspace="2" vspace="2" class="photo-img" src="'.$thumbnail.'" border="0" alt="'.$screenname.'" width="24" height="24" /></a>';

		//increment count

		$count++;

	} 

	//print out closing div tag

	echo "</div>";

}

function widget_myTwitterFollowers($args) {

  //make our widget a bit mor compatible with some other themes

  extract($args);

  echo $before_widget;

  echo $before_title;?>

  My Twitter Followers<?php echo $after_title;

  myTwitterFollowers();

  echo $after_widget;

}

function myTwitterFollowers_init()

{

  //regiester the sidebar widget

  register_sidebar_widget(__('myTwitterFollowers'), 'widget_myTwitterFollowers');

}

//Hook our initiater function to the wordpress plugins loaded event.

add_action("plugins_loaded", "myTwitterFollowers_init");

?>
5 Responses to “WordPress Twitter Followers Widget”
Yohan Perera Posted on September 29, 2010 at 1:19 pm

Good try… I faced the same problem that you encountered and couldn’t find a solution until stumbled upon this post.

Changed the user name in the code you provided, uploaded it to the server and everything works fine except that the thumbnails are too small…

Can you tell me how to increase the thumbnail size…

Yohan Perera Posted on September 29, 2010 at 1:26 pm

Hmmmm… Had to remove the plugin because of the following error appeared upon saving changes to the WP theme. Hope you will find this piece of information useful as you develop the thing…

Warning: Cannot modify header information – headers already sent by (output started at /home2/virtualp/public_html/wp-content/plugins/mytwitterfollowers.php:79) in /home2/virtualp/public_html/wp-admin/theme-editor.php on line 89

jason.white Posted on October 5, 2010 at 12:50 pm

Hi Yohan,

This only happens when you try to save the theme in admin? is that correct?

Many thanks for trying my solution.

De Hoify Posted on January 23, 2011 at 4:36 am

Hi, could you pleae tell me how to instsl this on my blog. Thanks

jason.white Posted on February 17, 2011 at 2:58 pm

Hi De,

Simply download the widget to your plugin directory, then from the plugin list within your wordpress installation click “Activate”

Enjoy :)

Post a Comment