This is quite a nice trick if you don’t like buttons such as Facebook Like or any Twitter counter buttons on your WordPress website or blog. With this method, you can display your Twitter count as normal text and it can blend in quite nicely if you have custom WordPress theme for your blog or website.
Create a new file and call it twittercounter.php and paste the below code into that file. Make sure the file is located in the root of your theme file.
{code type=PHP}
<?php
$tw = get_option(“twitterfollowerscount”);
if ($tw[‘lastcheck’] < ( mktime() – 3600 ) )
{
$xml=file_get_contents(‘http://twitter.com/users/show.xml?screen_name=antonrsa’);
if (preg_match(‘/followers_count>(.*)</’,$xml,$match)!=0) {
$tw[‘count’] = $match[1];
}
$tw[‘lastcheck’] = mktime();
update_option(“twitterfollowerscount”,$tw);
}
echo $tw[‘count’];
?>
{/code}
Make sure to replace antonrsa with your twitter name.
Then Simply place this code anywhere you want to display your Twitter counter:
{code type=PHP}
<?php include(“twittercounter.php”); ?>
{/code}
Blog post Re Tweets
If you would like to display your blog post re tweets without a plugin, use the code below on your blog post pages. Usually you can paste it in the file single.php located in the root of your theme files.
{code type=PHP}
<?php
$link = get_permalink($post->ID);
$key = ‘Backtype Key’;
$url = “http://api.backtype.com/tweetcount.xml?q=$link&key=$key”;
$request = new WP_Http;
$result = $request->request( $url );
echo “<span class=\”result\”>”.$result[‘body’].”</span> tweets”;
?>
{/code}
Replace the $key = ‘Backtype Key’ with your own key on Backtype.
Take a look at the Transients API:
http://codex.wordpress.org/Transients_API
Other than that, thanks for sharing.
Thanks. I’ll have a look at that API
How did you integrate the ReTeets on the hompage?
There’s a couple of ways that you can display your latest tweet. Have a look at this article for a “how to” – http://www.kristarella.com/2009/01/display-your-tweets-without-a-plugin/
Very useful snippet indeed. At times the default count button is boring. Thanks for sharing this post.
http://wpthemepower.com/