Skip to content

Instantly share code, notes, and snippets.

@paulruescher
Created March 22, 2013 19:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulruescher/5224004 to your computer and use it in GitHub Desktop.
Save paulruescher/5224004 to your computer and use it in GitHub Desktop.
Twitter Feed
// 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