-
-
Save jeremyclark13/2481191 to your computer and use it in GitHub Desktop.
<?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'); | |
?> |
How are you including this file? It relies on a couple of WordPress functions so it will need to either be put into a plugin file or included from your theme's function.php.
Ah, my bad – works now. I thought I could run it as a single file. Cheers.
Yeah it uses some wrapper functions for caching the request and doing the api request itself. Glad to hear you got it working.
Good to know. Also, if I'd like to remove the word "followers" from the count, what do I need to edit/remove?
Change line 44 from this
$output = "<a href='https://twitter.com/intent/user?screen_name={$user}' title='Follow Me on Twitter' target=\"_blank\">" . $data->followers_count ." followers";
to this
$output = "<a href='https://twitter.com/intent/user?screen_name={$user}' title='Follow Me on Twitter' target=\"_blank\">" . $data->followers_count;
Thanks again!
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?
$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 );
Hi
I'm getting this now:
Fatal error: Call to undefined function get_transient() in ....../twitter.php on line 19