Skip to content

Instantly share code, notes, and snippets.

@growdev
Forked from krogsgard/gist:5224775
Created March 29, 2013 13:46
Show Gist options
  • Save growdev/5270959 to your computer and use it in GitHub Desktop.
Save growdev/5270959 to your computer and use it in GitHub Desktop.
// Twitter Feed
add_shortcode( 'tweet-tweet', 'infom_get_twitter_feed' );
function infom_get_twitter_feed( $atts ) {
extract(shortcode_atts(array(
'handle' => 'krogsgard',
'count' => '1'
), $atts));
$handle = sanitize_text_field( $handle );
$count = intval( $count );
// Check transients
$tweet_body = get_transient( 'infom_twitter_data' );
if( empty( $tweet_body ) ) {
$twitter_url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=' . $handle . '&count=' . $count;
$data = wp_remote_get( $twitter_url );
$tweet_body = wp_remote_retrieve_body( $data );
if( empty( $tweet_body ) )
return false;
$tweet_body = json_decode( $tweet_body, true );
set_transient( 'infom_twitter_data', $tweet_body, 50 );
}
//Begin Output
$output = '<aside class="twitter-feed">';
// Work with decoded data
foreach( $tweet_body as $tweet ) {
$output .= '<div class="single-tweet">';
// $output .= '<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 .= make_clickable( $tweet['text'] );
$output .= '</div>';
}
$output .= '</aside>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment