Count facebook, twitter, and g + promotions together and save them in a database?

I worked on my Word Press word and got this idea. Instead of implementing the β€œLike / Fav” function to determine the best articles, I would instead calculate how many total Facebook sections, tweets and +1 received by this article, and after all of them are counted together, store them in the database (according to the corresponding article), so I can choose the best articles by selecting them with most stocks, tweets and +1. I will also need to update the database every time the user clicks on any of the facebook, twitter or g + buttons.

Is this achievable in Word Press and using their api?

+5
source share
1 answer

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;
            // do something with it
        }
    });
}

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;
            // do something with it
        }
    });
};

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;
            // do something with it
        }
    })
}

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

/*
    Plugin Name: Save Share Count Request Plugin
    Plugin URI: http://yourdomain.com/
    Description: This plugin catches 'save share count' requests and updates the database.
    Version: 1.0

*/

// check if request is a 'save share count' request
// i'm using sscrp_ prefix just not to redefine another function
// sscrp_ means SaveShareCountRequestPlugin_
function sscrp_is_save_share_count_request() {
    if(isset($_GET['_save_share_count_request'])) return true;
    else return false;
}

// save the count in database
function sscrp_save_share_count_in_wpdb($type, $count, $post_id) {

    // $count is your new count for the post with $post_id
    // $type is your social media type (can be e.g.: 'twitter', 'facebook', 'googleplus')
    // $post_id is post id

    global $wpdb;

    // create a $wpdb query and save $post_id $count for $type.
    // i will not show how to do it here, you have to work a little bit

    // return true or false, depending on query success.

    return false;
}

// catches the request, saves count and responds
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)));
        }
    }
}

// catch the save request just after wp is loaded
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) {
                // done!
            } else {
                // oh crap, an error occured
            }
        }
    });
    
  • 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. , , .

+8

All Articles