Twitter Snippets in wordpress

Posted on February 19th, 2011

Adding twitter snippets is an awesome idea to increase traffic to your website. This will attract more visitors. Here are some PHP snippets to interact from your website.

Twitter followers:

Code in function.php:

function follow($id){
$xml=file_get_contents(''.$ id);
if (preg_match('/followers_count>(.*)<!--',$xml,$match)!=0) {
$tw['count'] = $match[1];
return $tw['count'];

Calling the above function:

$followers = follow('phpsnippets');
echo $followers." followers!";

Twitter Status:

Now, lets take a look at how to display the twitter status in your website.It is pretty simple.

Add the below code in function.php

function get_status($twitter_id, $hyperlinks = true) {

$c = curl_init();
curl_setopt($c, CURLOPT_URL, "$twitter_id.xml?count=1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$src = curl_exec($c);
preg_match('/(.*)/', $src, $m);
$status = htmlentities($m[1]);
if( $hyperlinks ) $status = ereg_replace("[[:alpha:]]+://[^[:space:]]+[[:alnum:]/]", '\', $status);


Calling the above function:

echo get_status('username');