Extremely Simple Way To Display Delicious Counts For WordPress Users

Sep 3rd, 09 by Dicky | View Comments |
You maybe interested in our latest article Personal Facebook Fan Pages of Celebrities.

Delicious is the most popular social bookmarking service. If you pay attention on WDB, then you may notice that most of WDB’s articles are being saved more than 100 times by Delicious users.

There are a lot of ways for you to display your Delicious saved counts. But today we are going to show you how to use Delicious API together with WordPress custom field to save and display the Delicious saved counts.

What you need to know before start writing the code?

  1. MD5 – You need the URL’s Md5 hash value in order to lookup its Delicious saved counts. You can read more by reading the PHP md5() function.
  2. serialize() and unserialize(). We will need to serialize the data before storing to the database, and then unserialize them after retrieve from database.
  3. Custom field. We use the custom field to store the Delicious saved counts to the database. By saving the Delicious saved counts to the database, we can reduce the number of calls to Delicious’s API and also drastically speed up your post loading speed. Imagine, if you have 1k visitors per hour, then you will have 1000 requests sent to Delicious per hour. So, we have to save the counts in our database in order to reduce our server load too.
  4. 

Step by step to create your plugin

  1. We will have a function called delicious_count() and everything will be inside this function. We need the global $post variable and also another variable $count to store the Delicious saved counts.This is the initial setup for the function, which will return the saved counts when being called.
    function delicious_count() {
    global $post;
    $count = 0;
    
    return $count;
    }
  2. Here, we would like to introduce another variable called $old_del, which is an array that will contains the Delicious saved counts together with the “lastcheck” timestamp. We will look into more details about $old_del later.
  3. As i explain before, we save everything in the custom field called “_delicious”. The reason why i include the “_” in front of the custom field is to make it “invisible” so that nobody can go and edit the value, unless you login to your database server.
    $old_del = unserialize(get_post_meta($post->ID, '_delicious', true));

    After retrieve the value from database, we need to unserialize it into an array and pass to $old_del.

  4. Now, let’s look into the details of $old_del. It is a key-value pair array with 2 elements inside. the first one is “count” and the second one is “lastcheck”. The “lastcheck” indicates the time (in Unix timestamp) before the array being serialize and saved into the database. We will then use the mktime() method to get the current timestamp, and then subtract 600(600 seconds). This is to make sure we will not call the Delicious API within next 10 minutes.
    if($old_del == null || $old_del['lastcheck'] < (mktime() - 600)) {
    }
  5. Now, we reach the most important part – how to get the Delicious saved counts from the API.
    $jsonurl  = "http://feeds.delicious.com/v2/json/urlinfo/" . md5(get_permalink());
    $json = file_get_contents($jsonurl,0,null,null);
    $json_output = json_decode($json, true);
    $del_count =  $json_output[0]['total_posts'];
    $del['count'] = $del_count;
    $del['lastcheck'] = mktime();
    <$del = serialize($del);

    The first line is to construct the JSON URL while the lines 2-4 will retrieve the counts and saved into the variable $del_count. After that, we save both the Delicious counts and “lastcheck” into the variable $del and serialize it.

  6. After we get everything, it is the time for us to save them into the database. Here, we will need both update_post_meta() and add_post_meta(). You can refer them through WordPress Codex if you don’t know how to use.
    if($old_del != null) {
    update_post_meta($post->ID, '_delicious', $del);
    }
    else {
    add_post_meta($post->ID, '_delicious', $del, true);
    }
  7. We almost reach the final stage. Before we see how to use this function, let’s see the complete code here.
    <?php
    /*
    Plugin Name: Delicious Counts
    Version:     1.0
    Plugin URI:  http://www.webdesignbooth.com
    Description: Simple Delicious Counts Plugin For WordPress
    Author:      Dicky
    Author URI:  http://www.webdesignbooth.com
    */>
    function delicious_count() {
    global $post;
    $count = 0;
    $old_del = unserialize(get_post_meta($post->ID, '_delicious', true));
    
    if($old_del == null || $old_del['lastcheck'] < (mktime() - 600)) {
    $jsonurl  = "http://feeds.delicious.com/v2/json/urlinfo/" . md5(get_permalink());
    $json = file_get_contents($jsonurl,0,null,null);
    $json_output = json_decode($json, true);
    $del_count =  $json_output[0]['total_posts'];
    $del['count'] = $del_count;
    $del['lastcheck'] = mktime();
    $del = serialize($del);
    
    if($old_del != null) {
    update_post_meta($post->ID, '_delicious', $del);
    }
    else {
    add_post_meta($post->ID, '_delicious', $del, true);
    }
    $count = ($del_count == null) ? 0 : $del_count;
    }
    else {
    $count = ($old_del['count'] == null) ? 0 : $old_del['count'];
    }
    
    return $count;
    }
    
    ?>
  8. Ok, now you have the complete code.Simply call delicious_count() within the WordPress loop and you will have your Delicious saved counts. I hope you enjoy this tutorials as i think this method is pretty helpful and it demonstrates how to make use of the WordPress custom field.
We regularly update articles about resources, tutorials and Wordpress to help designers and developers. If you are new to WebDesignBooth, why not subscribe to our RSS feed and get the latest updates immediately. You can also subscribe through email or follow us on Twitter.
  • http://www.tripwiremagazine.com/news/news/60-fresh-very-useful-articles-for-designers.html 60+ Fresh Very Useful Articles for Designers | tripwire magazine

    [...] Extremely Simple Way To Display Delicious Counts For WordPress Users [...]

  • http://www.orphicpixel.com Mars

    great plugin tutorial, let me try this one out

  • http://cssbrigit.com/CNews/entrada/2009090317471155 CSS Brigit | Extremely Simple Way To Display Delicious Counts For Wordpress Users

    Extremely Simple Way To Display Delicious Counts For WordPress Users…

    This article teaches you how to retrieve and display Delicious saved counts to your readers. We also make use of WordPress custom field to reduce the number of calls to Delicious API….

  • http://design-newz.com/2009/09/04/extremely-simple-way-to-display-delicious-counts-for-wordpress-users/ Extremely Simple Way To Display Delicious Counts For Wordpress Users | Design Newz

    [...] Extremely Simple Way To Display Delicious Counts For WordPress Users [...]

  • http://www.bigthink.it/rubriche/il-meglio-della-settimana-28/ Il meglio della settimana #28 | BigThink

    [...] Extremely simple way to display Delicious counts Ffr WordPress users Uno script per mostrare quante volte un articolo è stato bookmarkato su Delicious. [...]

  • http://www.smejemesenainternetu.cz Zábavná videa

    Super hack. Nice work. Thx

  • http://www.techieblogger.com Techie Blogger

    really nice tutorial mate.. i tried it in my own blog.. and its working gr8

  • http://www.area1.info Opariuc Mihai

    What about Digg and other services? Could you write an article for the others too?

  • http://www.webdesignbooth.com Dicky

    Hi,
    What do you mean by “other services”? I haven’t try with Digg, but will definitely give it a try in future. Thanks for your suggestion. :)

  • http://www.area1.info Opariuc Mihai

    By “other services” I mean Stumble, Mixx and so on. I think you’ve got the point ;)

    Thanks for answering, have a nice day!

  • http://stefanm.wordpress.com/2009/09/21/cool-articles-%e2%80%93-seo-blogging-internet-marketingseptember-07-20-2009/ Cool articles – SEO, blogging, internet marketing(september 07-20, 2009) « Stefanm, my link collection

    [...] Step by step to create your plugin: Display Delicious Counts For WordPress Users, via @webdesignbooth; [...]

  • http://www.uswowgold.com/ Alex

    Delicious is very useful for share your bookmark online.

  • http://www.insidethewebb.com/ Inside the Webb

    I’ve been looking to try and set this up on my blog for ages, but I couldn’t ever figure out how to do it. Glad I stumbled into this post, great tutorial and I’ll definitely be back for more :)

  • http://www.freshwebmedia.com.au/ web design perth

    I know about the Html commands about the tags but this is new for my knowledge and it really works. Now I use these commands mentioned in the post in my work. Nice info there. Thank you.

blog comments powered by Disqus