Created
April 24, 2012 16:25
-
-
Save jeremyclark13/2481191 to your computer and use it in GitHub Desktop.
WordPress simple twitter follower count function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple twitter feed | |
* | |
* @param string $user user of twitter feed to retrieve. | |
* | |
* @return string of formatted API data | |
*/ | |
function twitter_followers($user = 'clarktechnet'){ | |
// Build Twitter api url | |
$apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}"; | |
//cache request | |
$transient_key = $user . "_twitter_followers"; | |
// If cached (transient) data are used, output an HTML | |
// comment indicating such | |
$cached = get_transient( $transient_key ); | |
if ( false !== $cached ) { | |
return $cached; | |
} | |
// Request the API data, using the constructed URL | |
$remote = wp_remote_get( esc_url( $apiurl ) ); | |
// If the API data request results in an error, return | |
// an appropriate comment | |
if ( is_wp_error( $remote ) ) { | |
return '<p>Twitter unaviable</p>'; | |
} | |
// If the API returns a server error in response, output | |
// an error message indicating the server response. | |
if ( '200' != $remote['response']['code'] ) { | |
return '<p>Twitter responded with an HTTP status code of '. esc_html( $remote['response']['code']) . '</p>'; | |
} | |
// If the API returns a valid response, the data will be | |
// json-encoded; so decode it. | |
$data = json_decode( $remote['body'] ); | |
$output = "<a href='https://twitter.com/intent/user?screen_name={$user}' title='Follow Me on Twitter' target=\"_blank\">" . $data->followers_count ." followers"; | |
set_transient( $transient_key, $output, 600 ); | |
return $output; | |
} | |
//Example Shortcode | |
add_shortcode( 'twitter', 'twitter_followers_cb' ); | |
function twitter_followers_cb( $atts) { | |
extract( shortcode_atts( array( | |
'user' => '' | |
), $atts ) ); | |
return twitter_followers($user); | |
} | |
// [twitter user="clarktechnet"] | |
// Template usage | |
echo twitter_followers('clarktechnet'); | |
?> |
$output = $user . " " . $data->followers_count ." followers";
This should work.
For some reason it's giving me "username XXXX followers".
So I tried this with no luck:
$output = $data->followers_count;
EDIT: Sorry, seems to work now. I just had to clear my cache a few times.
One thing to note is that if you've been doing testing this might be a cached result, from an earlier attempt. Comment out this line:
$cached = get_transient( $transient_key );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One last thing from me:
I'm trying to make it output clean text with no a href on the number.
I tried this with no luck:
$output = $user . $data->followers_count ." followers";
Any tips?