Seguidores de Twitter en WordPress
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?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; } //usage echo twitter_followers('clarktechnet'); ?> |
Fuente: http://clark-technet.com/2012/04/simple-wordpress-twitter-follower-count-snippet