0

I recently developed a news blog, and my client needs facebook share count. I Successfully able to get the share count and update the custom field using below code.(with the help of cybermeta). What this code does is, it loops through all the available posts and gets the share count from facebook api and updates the individual post's custom field.

register_activation_hook( __FILE__, 'cb_fb_share_count_activation' );
    function cb_fb_share_count_activation() {
        wp_schedule_event( time(), 'hourly', 'cb_update_fb_share_count' );
    }

    register_deactivation_hook( __FILE__, 'cb_delete_fb_share_count_schedule' );
    function cb_delete_fb_share_count_schedule() {
        wp_clear_scheduled_hook( 'cb_update_fb_share_count' );
    }

    add_action( 'cb_update_fb_share_count', 'cb_update_count' );
    function cb_update_count(){
        $posts = get_posts(array('numberposts' => -1) );
        foreach($posts as $post) {
            $url = get_permalink( $post->ID );
            $response = wp_remote_get('https://api.facebook.com/method/links.getStats?urls='.$url.'&format=json' );
            if( ! is_wp_error( $response ) ) {
                $fbcount = json_decode( wp_remote_retrieve_body( $response ) );
                $fb_share_count = $fbcount[0]->share_count; 
                update_post_meta( $post->ID, 'cb_fb_share_count', $fb_share_count );
            } else {
                //Do something if it was an error comunicating with Facebook
            }
        }

    }

However, It does'nt seem to be a good way to get share count and updating custom field.

Is there any better approch? I don't want any problem even if the blog has 2000 articles in the long run.

1 Answer 1

1

I've actually made a plugin called Social Metrics Tracker which does exactly what you are trying to achieve. Please also feel free to read the source code on GitHub.

To answer your questions:

You are right that running an hourly cron task to update all of the posts would cause a problem with even just 200 articles because the server would make one GET request for each post every hour to the Facebook API.

Here is what I learned and what I did for my implementation:

1. Only run the update task if there is actually traffic to a post.

There is no reason to update stats for an old post with no visitors. Instead of running the update check every hour for every post, in my implementation I run the update check in the head of each individual post.

2. Use a TTL to prevent frequent updates

Store and retrieve the timestamp every time that we update stats for a given post. This way, a post won't be updated more frequently than every 3600 seconds (for example). This value is configurable since blogs with a high volume of posts will need a longer TTL.

3. Run the update in the cron

In your example above, you are already running the update in the cron (which is good). In my implementation, updates are also run in the cron to prevent additional page load time for site visitors.

Here is some example code which takes into account the three considerations above:

add_action( 'wp_head', 'checkThisPost');

public function checkThisPost() {
    global $post;
    if ($post) $post_id = $post->ID;

    $last_updated = get_post_meta($post_id, "socialcount_LAST_UPDATED", true);

    // Just an example of how this works:
    if ($last_updated > time()-3600) {
        wp_schedule_single_event( time(), 'social_metrics_update_single_post', array( $post_id ) );
    }

}

function social_metrics_update_single_post($post_id) {
    // Run the update!
}
2
  • Awesome :)...Does it play well with cache plugins? Apr 25, 2015 at 3:08
  • I'm actually not 100% sure how well it integrates with caching. The check is done in the wp_head action hook, so I guess it depends if that hook gets executed when a cache page is served. I would imagine with plugin based caching (like W3TC) the hook still gets executed. But with server-level caching, the hook might not get executed. At the very least, if the cache expires every 24 hours then an update can be scheduled every 24 hours when a new version of the page is requested.
    – Ben Cole
    Apr 26, 2015 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.