Showing off your FeedBurner subscribers

November 20th, 2008 by advinci

Another great post from http://yoast.com

Quite a few websites have started to show of their FeedBurner subscriber count with something else than a widget. To do that, you have to be able to grab the number of subscribers through the FeedBurner API.

Now I bet you want to know how! It’s bloody easy in WordPress, just do this:

$fb = get_option("feedburnersubscribecount");
if ($fb['lastcheck'] < ( mktime() - 600 ) ) {
	$snoopy = new Snoopy;
	$result = $snoopy->fetch("http://api.feedburner.com/awareness/1.0/GetFeedData?uri=feedburnerid");
	if ($result) {
		preg_match('/circulation=\"([0-9]+)\"/',$snoopy->results, $matches);
		if ($matches[1] != 0)
			$fb['count'] = $matches[1];
		$fb['lastcheck'] = mktime();
		update_option("feedburnersubscribecount",$fb);
	}
}
echo ''.$fb['count'].' Subscribers can\'t be wrong! Subscribe by <a href="http://yoast.com/email-blog-updates/">email</a> or to the <a rel="nofollow" href="http://feeds.feedburner.com/joostdevalk">RSS feed</a> now!';

Copy, paste, replace feedburnerid with your own FeedBurner ID, and you’re done!

Update: updated the code to include caching, as you might overload the FeedBurner API otherwise.

Update 2: if you get an error saying it can’t find the class Snoopy, add this above the code:

require_once(ABSPATH . 'wp-includes/class-snoopy.php');

How to display your last tweet in WordPress

November 17th, 2008 by advinci

I found this great post here. Check this blog out if you want more wordpress advice.

I wanted to show my latest tweet on the front page of this site, and although I know there are several plugins which probably could’ve helped me do this, I decided to see how easy the API was to use.

It turned out to be incredibly easy, as long as you have PHP 5.2 or higher, that is.

This is the code:

require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$snoopy = new Snoopy;
$snoopy->fetch("http://twitter.com/statuses/user_timeline/jdevalk.json?count=1");
$twitterdata = json_decode($snoopy->results,true);
echo "<p>\"".$twitterdata[0]["text"]."\"</p>";

As you can see, I use the Snoopy library to fetch the data, as that comes with WordPress by default. Than I decode the JSON results by using the json_decode function. This is the reason you need PHP 5.2 or up, as this was only included with this version of PHP.

Next, we output it. Of course, this is a quick & dirty implementation. If I got dugg now, and 1,000 people a minute came looking at that page, it should have some sort of caching in there. For now though, this is fine as it is!

Update: As Kim noticed in the comments, I’ve added some code to automatically link any @username mentions to those usernames. Replace the last echo line above with this:

$pattern = '/\@([a-zA-Z]+)/';
$replace = '<a href="http://twitter.com/'.strtolower('\1').'">@\1</a>';
$output = preg_replace($pattern,$replace,$twitterdata[0]["text"]);
echo "<p>\"".$output."\"</p>";

You could even decide to add rel="nofollow" the $replace if you don’t want those links to give juice.

Update 2: To make it even more complex, here is the entire code I now use, which excludes replies and caches the Twitter API requests so you won’t overload the API:

require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$tweet   = get_option("lasttweet");
$url  = "http://twitter.com/statuses/user_timeline/jdevalk.json?count=20";
if ($tweet['lastcheck'] < ( mktime() - 60 ) ) {
  $snoopy = new Snoopy;
  $result = $snoopy->fetch($url);
  if ($result) {
    $twitterdata   = json_decode($snoopy->results,true);
    $i = 0;
    while ($twitterdata[$i]['in_reply_to_user_id'] != '') {
      $i++;
    }
    $pattern  = '/\@([a-zA-Z]+)/';
    $replace  = '<a href="http://twitter.com/'.strtolower('\1').'">@\1</a>';
    $output   = preg_replace($pattern,$replace,$twitterdata[$i]["text"]);  

    $tweet['lastcheck'] = mktime();
    $tweet['data']    = $output;
    $tweet['rawdata']  = $twitterdata;
    $tweet['followers'] = $twitterdata[0]['user']['followers_count'];
    update_option('lasttweet',$tweet);
  } else {
    echo "Twitter API not responding.";
  }
} else {
  $output = $tweet['data'];
}
echo "<p>\"".$output."\"</p>";

As you can see it also saves the amount of followers you have into $tweet['followers'], which I then use to display this:

<p>
  <a href="http://twitter.com/jdevalk">
    <?=$tweet['followers']?> followers on Twitter, and you?
  </a>
</p>

Update 3: Another fix to automatically make all links clickable, below this line:

$output = preg_replace($pattern,$replace,$twitterdata[$i]["text"]);

Add:

$output = make_clickable($output);

This will use the WordPress internal make_clickable function to make sure that all URL’s are clickable.

Enjoy!

Which hosting I use for WordPress blogs?

October 17th, 2008 by advinci

I have been using HostGator.com over a year now and am more than happy with it.

Why I like HostGator:

  • WordPress 1 click installation. Hostgator is famous for its one-click installations (WP, Joomla, forums etc).
  • Great prices. I use a hosting plan for $7.95 a month for my regular WP blogs and a Reseller account for $24.95 for my WP MU sites.
  • Unlimited domains. All my websites are under one account, costing only the price you see above.
  • Unlimited disk space and bandwidth. It’s probably not really unlimited but I having several websites under this account haven’t had any problems yet.
  • PHP 5 and mod_rewrite. Many WordPress users have problems because their hosting doesn’t support php5 and mod_rewrite. No problems here.
  • Live support. If you have any problems or questions you can use live chat for support. They don’t know all the answers but they’ve helped me several times.

So if you’re looking for a good shared hosting then HostGator is one that I recommend. You can check out different hosting packages here.

NB! If you want to install WordPress MU and BuddyPress you will need a Reseller account to configure wildcard DNS in order to make subdomains work. Though WP MU works on a regular account too, but only with subfolders instead of subdomains.

Hello!

October 17th, 2008 by advinci

I made an account to test BuddyPress. But I guess I might post here a thing or two related to WordPress, since I’m already registered here.