Skip to content

Instantly share code, notes, and snippets.

@wilsonwc
Last active December 18, 2015 10:49
Show Gist options
  • Save wilsonwc/5771548 to your computer and use it in GitHub Desktop.
Save wilsonwc/5771548 to your computer and use it in GitHub Desktop.
Get and cache twitter follower count in php and wordpress
<?php
function update_facebook_count() {
$name = 'giantsydney';
$optionName = $name . '_facebook_followers';
$url = 'https://graph.facebook.com/'.$name;
$fb = @json_decode(file_get_contents($url));
$count = number_format($fb->likes);
add_option($optionName);
update_option($optionName, $count);
}
function fb_count($name) {
$optionName = $name . '_facebook_followers';
echo get_option($optionName);
}
function update_twitter_count() {
$name = 'giantsydney';
$url = 'https://twitter.com/users/show/'. $name;
$optionName = $name . '_twitter_followers';
$response = file_get_contents ( $url );
$t_profile = new SimpleXMLElement ( $response );
$count = $t_profile->followers_count;
$count = (float) $count;
$count = number_format($count);
add_option($optionName);
update_option($optionName, $count);
}
function twitter_count($name) {
$optionName = $name . '_twitter_followers';
echo get_option($optionName);
}
function update_social_count(){
update_facebook_count();
update_twitter_count();
}
if (!wp_next_scheduled('tw_update_hourly')) {
wp_schedule_event(time(), 'hourly', 'tw_update_hourly');
}
add_action('tw_update_hourly', 'update_social_count');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment