0

I recently developed a news blog. My client asked for a facebook share count of an article.

I am able to get the share count using facebook api, However, according to the information I found from various questions from here and there, facebook limits the number of API calls. So, i have decided to pull the share count information every hour then update every post share count information using a custom meta field. I used wp_schedule_event for automation. But, Some how the following code isn't working.

register_activation_hook( __FILE__, 'cb_fb_share_count_activation' );
function cb_fb_share_count_activation() {
    $timestamp = wp_next_scheduled( 'cb_update_fb_share_count' );
    if( $timestamp == false ){
       wp_schedule_event( time(), 'hourly', '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) {
        setup_postdata($post);
        $url = get_the_ID();
        $fbcount = json_decode( file_get_contents( 'https://api.facebook.com/method/links.getStats?urls='.$url.'&format=json' ));
        $fb_share_count = $fbcount[0]->share_count; 
        update_post_meta(get_the_ID(), 'cb_fb_share_count', $fb_share_count);
    }
    wp_reset_postdata();
}
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' );
}

I then tried 'wp_footer' to test the get_posts and It's working. Whats wrong with the custom hook ? Where am I doing it wrong?

P.S: I am aware that Wp-cron requires a page view to operate properly.

1

It think that your problem is the you are passing the post ID as url parameter to facebook API:

$url = get_the_ID();

Also, as get_posts return an array of post objects, you could use this code:

$posts = get_posts(array('numberposts' => -1) );
foreach($posts as $post) {
    $url = get_permalink( $post->ID );
    $fbcount = json_decode( file_get_contents( 'https://api.facebook.com/method/links.getStats?urls='.$url.'&format=json' ) );
    $fb_share_count = $fbcount[0]->share_count; 
    update_post_meta( $post->ID, 'cb_fb_share_count', $fb_share_count );
}

I would go further and suggest to remove the checking of wp_next_scheduled in the activation action hook, it is not needed because at plugin activatition the scheduled event is supposed to not exists. And finally, I suggest to use wp_remote_get instead of file_get_contents, I think it is really better and have more control about the request:

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

        }

    }

}

PD: I think this is no very good approach. Get all posts and connect to Facebook one by one, all in the same WordPress load, can be very slow and intense.

  • Thank you so much :)...and then what could be a good approch?...can I use the_content filter instead of custom schedule hook? – Naresh Devineni Mar 15 '15 at 4:53
  • I don't know what approach would be better, I would think about it. But yours I think is not. Imaging you have, lets say 1000 posts; in one single cron you get all of them, connect to facebook 1000 times and run update_post_meta 1000 times. It is very probably that most shared hosting and small VPS enviroment will crash or go over max time execution timeout. – cybmeta Mar 15 '15 at 10:03
  • Right now he just had 50 posts, may be 300 in next three months. I will post another question here. Thank you very much for solving my problem :) – Naresh Devineni Mar 15 '15 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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