Created
March 22, 2013 19:25
-
-
Save paulruescher/5224004 to your computer and use it in GitHub Desktop.
Twitter Feed
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
// Twitter Feed | |
add_shortcode( 'twitter', 'rl_twitter_feed' ); | |
function rl_twitter_feed( $atts ) { | |
// Attributes | |
if( isset( $atts['handle'] ) ) | |
$handle = $atts['handle']; | |
// Check transients | |
$body = get_transient( 'rl_twitter_data' ); | |
if( empty( $body ) ) { | |
$twitter_url = 'http://twitter.com/statuses/user_timeline/' . $handle . '.json?count=3'; | |
$data = wp_remote_get( $twitter_url ); | |
$body = wp_remote_retrieve_body( $data ); | |
if( empty( $body ) ) | |
return false; | |
$body = json_decode( $body, true ); | |
set_transient( 'rl_twitter_data', $body, 300 ); | |
} | |
//Begin Output | |
$output = '<aside class="twitter-feed"><ul>'; | |
// Work with decoded data | |
foreach( $body as $tweet ) { | |
$output .= '<li>'; | |
$output .= '<p><span class="twitter-meta"><time datetime="' . date( 'Y-m-d', strtotime( $tweet['created_at'] ) ) . '">' . date( 'd F, Y', strtotime( $tweet['created_at'] ) ) . '</time>'; | |
$output .= ' - <a href="http://twitter.com/' . $tweet['user']['screen_name'] . '">@' . $tweet['user']['screen_name'] . '</a></span>'; | |
$output .= $tweet['text']; | |
$output .= '<span class="twitter-source">Via ' . $tweet['source'] . '</span>'; | |
$output .= '</p></li>'; | |
} | |
$output .= '</ul></aside>'; | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment