Skip to content

Instantly share code, notes, and snippets.

@jeremyclark13
Created April 24, 2012 16:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyclark13/2481191 to your computer and use it in GitHub Desktop.
Save jeremyclark13/2481191 to your computer and use it in GitHub Desktop.
WordPress simple twitter follower count function
<?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');
?>
@atlemo
Copy link

atlemo commented Jul 9, 2012

Hi
I'm getting this now:

Fatal error: Call to undefined function get_transient() in ....../twitter.php on line 19

@jeremyclark13
Copy link
Author

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.

@atlemo
Copy link

atlemo commented Jul 9, 2012

Ah, my bad – works now. I thought I could run it as a single file. Cheers.

@jeremyclark13
Copy link
Author

Yeah it uses some wrapper functions for caching the request and doing the api request itself. Glad to hear you got it working.

@atlemo
Copy link

atlemo commented Jul 9, 2012

Good to know. Also, if I'd like to remove the word "followers" from the count, what do I need to edit/remove?

@jeremyclark13
Copy link
Author

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;

@atlemo
Copy link

atlemo commented Jul 9, 2012

Thanks again!

@atlemo
Copy link

atlemo commented Jul 10, 2012

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?

@jeremyclark13
Copy link
Author

$output = $user . " " . $data->followers_count ." followers";

This should work.

@atlemo
Copy link

atlemo commented Jul 10, 2012

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.

@jeremyclark13
Copy link
Author

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