Developing a WordPress Blog Post Scoring System

WordPress blogs are certainly the best blogs out there, but wouldn’t it be great if you can create custom features for your WordPress blog posts? I was thinking about this for a long time, which led me to the creation of a new type of feature for my blog posts, a scoring system that automatically calculates your post’s score based on the number of Facebook likes, Tweets, Google +1s, and more activities, and displays it in your blog post in real time.

Creating a WordPress Blog Post Scoring System

In this tutorial I will be showing you how to create such a system for your WordPress blog, using API call, and a simple plugin to track post view count, and since the score will be generated dynamically on every page load, there’s no need to flood your database with scores, and also this dynamic calculation is fast, and doesn’t slow down your site. So, let the development begin.

Scoring Outline

First thing’s first, let’s fix the points we will be assigning for each activity, which will add up to calculate our post score. For this tutorial, I will be assigning these points for the following activities, but you may customize it according to your needs:

  • One Facebook Like/Share = 10 Points
  • One Tweet = 10 Points
  • One Google +1 = 10 Points
  • One Comment = 5 Points
  • One Post View = 5 Points

Now that we have fixed the points that we will give for each activity, it’s time to create some functions for each activity, so that we can use those functions to calculate the score for a particular activity.

Creating the Functions

Now for fetching the number of likes/tweets or other activities for a particular post, we need to create some functions that use API to fetch the activity on that particular post URL, and return it as an integer value.

Here are the functions that you need to copy and paste into your functions.php file for your currently active theme of your WordPress blog. You can edit your functions.php file by accessing your server using an FTP client, or here’s an easier way to do it. Just follow the step indicated below to open up the WordPress Theme Editor, and then choose functions.php file from the right hand side column.

Theme Editor

Fetching Facebook Like Count

function get_likes($url) {
 
    $json_string = file_get_contents('http://graph.facebook.com/?ids=' . $url);
    $json = json_decode($json_string, true);
 
    return intval( $json[$url]['shares'] );
}

This function will return the number of likes for a given URL which is to be passed using the $url variable as given in the function, using JSON.

Fetching Tweet Count

function get_tweets($url) {
 
    $json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
    $json = json_decode($json_string, true);
 
    return intval( $json['count'] );
}

Like the previous function, this function will fetch the number of tweets for a given URL, and return it.

Fetching Google +1 Count

function get_plusones($url) {
 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    $curl_results = curl_exec ($curl);
    curl_close ($curl);
 
    $json = json_decode($curl_results, true);
 
    return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}

Unlike the other two functions which are rather simple, this function is a little complicated, as Google+ returns the JSON query in a complicated format, we need multiple fetching algorithms, to fetch the desired data, which in our case is the number of +1s for a given URL.

Fetching Comments Count
For fetching the number of comments on a given post, we don’t need to build a custom function for this case, as WordPress already has a built-in function to get the number of comments for a particular post, so we just need to call it by providing the current post URL.

An example of such a call would be:
get_comments_number(get_the_ID())

Here the function get_the_ID() gets the post ID for the current post.

Note: Do not place this function in the functions.php file, we will place it later when calculating the final score in the single.php file.

Fetching Post View Count
For this feature we need to install a simple plugin to track the number of post views and return the number of post views for a particular post when we need it.

Install this plugin, Post Views Count for WordPress, and activate it. Now this plugin allows the display of post view count for a particular post using the shortcode post_view. But we cannot use shortcodes directly in the single.php file, so we need to convert the shortcode into its PHP equivalent. This can be done using this simple function, which is also available as built-in function for WordPress, do_shortcode("[post_view]").

This functions converts any given shortcode into its PHP equivalent, and can be used in any PHP file. Note – Do not place this function in the functions.php file, we will place it later in single.php file just like the previous function.

Calculating the Final Score

Now that we have created the separate functions for fetching the number of likes, tweets, +1s, comments and post views, it’s time to multiply each with their respective denominations, and calculate the final score for a post.

The code for calling the functions and calculating the final score will be:

<?php echo (get_tweets(get_permalink())*10) + (get_plusones(get_permalink())*10) + (get_likes(get_permalink())*10) + (do_shortcode("[post_view]")*5) + (get_comments_number(get_the_ID())*5); ?>

Here the function get_permalink() is a WordPress built-in function which returns the current post URL, which is then passed to the functions we have created before, and thus the score is calculated. You can see that at the time of calling each function, the result of the function is multiplied with the points for each activity, which we had previously discussed.

Displaying the Post Score

Now that we have calculated the score for a post, it’s time to display it to the visitor. You can display the score wherever you want in your single.php file, or your post page.

Post Score

In this tutorial I have displayed the score just beside the post title, so I have placed the code that we created in the previous step beside the code for post title in the single.php file. For example, in my case it was:

<h1 class="title single-title"><?php the_title(); ?></h1>&nbsp&nbsp<b><?php echo (get_tweets(get_permalink())*10) + (get_plusones(get_permalink())*10) + (get_likes(get_permalink())*10) + (do_shortcode("[post_view]")*5) + (get_comments_number(get_the_ID())*5); ?></b>

Here, this code:

<h1 class="title single-title"><?php the_title(); ?></h1>

Generates the title for the post, and the rest of code returns the calculated post score. The code for title generation may differ from theme to theme, but you can look at the function the_title() in the single.php, which actually indicates the title generation code, and you can place your created code just beside it, as we have done here.

Final Thoughts

So, we have created the scoring system, and have also displayed the score to the visitor for each post, now it’s up to you to make this score more creative, by styling it, varying the points and other things.

Therefore, we come to the end of this tutorial, and I would be glad to have a look at your scoring system, so do leave your links in the comments section below.

Opinions expressed in this article are those of the author and not necessarily those of Onextrapixel.