Advertisement
Guest User

FB Script

a guest
Jan 17th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | None | 0 0
  1. //FACEBOOK
  2. function insert_facebook_likes_custom_field($post_ID) {
  3.         global $wpdb;
  4.         if(!wp_is_post_revision($post_ID)) {
  5.             add_post_meta($post_ID, 'fb_likes_count', '0', true);
  6.         }
  7.     }
  8.  
  9. add_action('publish_page', 'insert_facebook_likes_custom_field');
  10. add_action('publish_post', 'insert_facebook_likes_custom_field');
  11.  
  12. add_action( 'fb_hourly_count', 'do_this_hourly' );
  13. add_action( 'wp', 'fb_activation' );
  14.  
  15. function do_this_hourly() {
  16.    
  17.         $args=array(
  18.         'post_type' => 'post',
  19.             'post_mime_type'   => '',
  20.             'post_status'      => 'publish'
  21.         );
  22.         $posts_array = get_posts( $args );
  23.        
  24.     foreach($posts_array as $post):
  25.                
  26.         $permalink = get_permalink($post->ID);
  27.        
  28.         $idpost = $post->ID;
  29.  
  30.         $data = file_get_contents('http://graph.facebook.com/?id='.$permalink);
  31.  
  32.         $json = $data;
  33.  
  34.         $obj = json_decode($json);
  35.        
  36.         $like_no = $obj->{'shares'};
  37.        
  38.         $meta_values = get_post_meta($idpost, 'fb_likes_count', true);
  39.  
  40.         if($like_no == null){$like_no = 0;}
  41.  
  42.         update_post_meta($idpost, 'fb_likes_count', $like_no, false);
  43.        
  44.     endforeach;
  45.  
  46. }
  47.  
  48. add_action('init', 'do_this_hourly');
  49.  
  50.  function fb_activation() {
  51.     if ( !wp_next_scheduled( 'fb_hourly_count' ) ) {
  52.         wp_schedule_event( time(), 'hourly', 'fb_hourly_count' );
  53.     }
  54.  }
  55.      /* End of fb like count management */
  56.  
  57. // END FACEBOOK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement