Advertisement

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.

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.