It is not as simple as it seems.
GitHub makes a lot of sense with all the APIs you want to implement: Get the number of views from various APIs .
You can connect to these servers and get data using jQuery and AJAX:
function how_many_tweets(url, post_id) {
var api_url = "http://cdn.api.twitter.com/1/urls/count.json";
var just_url = url || document.location.href;
$.ajax({
url: api_url + "?callback=?&url=" + just_url,
dataType: 'json',
success: function(data) {
var tweets_count = data.count;
}
});
}
function how_many_fb_shares(url, post_id) {
var api_url = "http://api.facebook.com/restserver.php";
var just_url = url || document.location.href;
$.ajax({
url: api_url + "?method=links.getStats&format=json&urls=" + just_url,
dataType: 'json',
success: function(data) {
var shares_count = data[0].total_count;
}
});
};
function how_many_google_pluses(url, api_key, post_id) {
var api_url = "https://clients6.google.com/rpc?key=" + api_key;
var just_url = url || document.location.href;
$.ajax({
url: api_url,
dataType: 'json',
contentType: 'application/json',
type: 'POST',
processData: false,
data: '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' + just_url + '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]',
success: function(data) {
var google_pluses = data.result.metadata.globalCounts.count;
}
})
}
You can then replace the strings with // do something with itanother AJAX request on your blog. You will need to write a plugin to handle this request and save the data in $ wpdb. The plugin is relatively simple:
<?php
function sscrp_is_save_share_count_request() {
if(isset($_GET['_save_share_count_request'])) return true;
else return false;
}
function sscrp_save_share_count_in_wpdb($type, $count, $post_id) {
global $wpdb;
return false;
}
function sscrp_catch_save_share_count_request() {
if(sscrp_is_save_share_count_request()) {
if(isset($_GET['type'])) {
$social_media_type = $_GET['type'];
$new_count = $_GET['value'];
$post_id = $_GET['post_id'];
if(sscrp_save_share_count_in_wpdb($social_media_type, $new_count, $post_id)) {
header(sprintf('Content-type: %s', 'application/json'));
die(json_encode(array("sscrp_saved"=>true)));
} else {
header(sprintf('Content-type: %s', 'application/json'));
die(json_encode(array("sscrp_saved"=>false)));
}
} else {
header(sprintf('Content-type: %s', 'application/json'));
die(json_encode(array("sscrp_saved"=>false)));
}
}
}
add_action('wp_loaded', 'sscrp_catch_save_share_count_request');
?>
When you have the plugin, we can edit the lines // do something with itin your JavaScript file:
For how_many_tweets()it will be:
$.ajax({
url: "http://yourdomain.com/path_to_your_wp_installation/?_save_share_count_request=1&type=twitter&value=" + tweets_count + "&post_id=" + post_id,
dataType: 'json',
success: function(data) {
var saved = data.sscrp_saved;
if(saved) {
} else {
}
}
});
To how_many_fb_shares()copy / paste the code from how_many_tweets()and just change:
...
url: "... &type=facebook ...
...
To how_many_google_pluses()do the same as with facebook:
...
url: "... &type=googleplus ...
...
- , $type $count, $wpdb.
. , Facebook, Twitter API Google. , , .