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 [1] which make sure that the twitter statues are loaded fast from the cache, also it makes use of the in built HTTP API [2] 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 [3]
- 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 $tweet->text; //gets the time the status was created $tweet->created_at; //gets the User Profile Image URL $tweet->user->profile_image_url; //gets the User Screen Name $tweet->user->screen_name; //gets the User location $tweet->user->location; //gets the User Name $tweet->user->name; //Get the follower count. $tweet->user->followers_count; //Get the User Website URL. $tweet->user->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; } else { return false; } } /* Credit Jeremy Parrish http://rrish.org/ */ function linkify($status_text) { // linkify URLs $status_text = preg_replace( '/(https?:\/\/\S+)/', '<a href="\1">\1</a>', $status_text ); // linkify twitter users $status_text = preg_replace( '/(^|\s)@(\w+)/', '\1@<a href="http://twitter.com/\2">\2</a>', $status_text ); // linkify tags $status_text = preg_replace( '/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', $status_text ); return $status_text; }