Learn, Share, Inspire, Ideas.

In my previous post I had outlined a solution to display your latest Twitter Posts in WordPress using JavaScript, I have worked out a PHP function that you can use to show everyone your latest Twitter Status. All you need to do is to call the Twitter PHP Function where you want to use on your WordPress site. You can even use it in your SideBar Widget.

One of the advantages of using this function is that, it makes use of WordPress inbuilt Caching mechanism for data called Transients API which make sure that the twitter statues are loaded fast from the cache, also it makes use of the in built HTTP API in WordPress to get the best possible method to do a HTTP request.

Adding Twitter In WordPress

Copy and paste the two functions given below into your wordpress functions.php theme file, in case that file is not there create it and paste the code.
Call getTwitterStatus('arpitjacob', '5'); where you want to show the twitter status.

  • The first value is your Twitter UserName.
  • The second one is the number of tweets to show.

Customizing the Twitter Status

While the functions outputs just the statues wrapped in a DIV you can configure it to show the following list of items. To get these values take a look at the comments in the functions. for more values take a look at the Twitter-REST-API-Method

  • The time the status was created
  • User Profile Image URL
  • User Screen Name
  • User Name
  • User Location
  • User Website URL
  • Follower count
function getTwitterStatus($name, $count)

	$transient = "$name"."_$count";
	//Get Tweets From the Cache
	$getTweets = get_transient($transient);
	if ($getTweets)
		echo $getTweets;
	elseif ($name != false)
		$site = "http://twitter.com/statuses/user_timeline.json?screen_name=$name&count=$count";
		//Perform Http request to get JSON feed of Twitter User Status
		$result = wp_remote_get($site);
		$json = $result['body'];
		//Convert JSON String to PHP Array
		$tweets = json_decode($json);
		$getTweets = '';

		foreach ( (array) $tweets as $tweet)
			Use these values to customize your output.
			//gets twitter status
			//gets the time the status was created
			//gets the User Profile Image URL
			//gets the User Screen Name
			//gets the User location
			//gets the User Name
			//Get the follower count.
			//Get the User Website URL.

			// Convert twitter Usernames and links to Hyperlinks
			$tweetcontent = linkify($tweet->text);
			$getTweets .= "<div> $tweetcontent </div>";

		//Set Cache if it does not exist
		set_transient($transient, $getTweets, 10);
		echo $getTweets;
		return false;

/* Credit Jeremy Parrish http://rrish.org/  */
function linkify($status_text)
  // linkify URLs
  $status_text = preg_replace(
    '<a href="\1">\1</a>',

  // linkify twitter users
  $status_text = preg_replace(
    '\1@<a href="http://twitter.com/\2">\2</a>',

  // linkify tags
  $status_text = preg_replace(
    '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>',

  return $status_text;

One Comments
  1. Not sure what I’ve done wrong here. I put it into my functions.php and called it on a page. The first time I looked at it, it worked like a charm… I didn’t change a thing, but now each time I reload it, it only outputs empty s.