Create a gist now

Instantly share code, notes, and snippets.

Newly updated WordPress Twitter functions to work with API v1.1
<?php
/**
* Example of use. Create array with auth tokens from application created from https://dev.twitter.com/apps/new
*
*/
$twitter_auth = array(
'consumer_key' => 'pHUfo9zXAtCVjXrsfaetatA',
'consumer_secret' => '0oddga08872ptXqajjpqld6iVbHziaBHKLTzdq1o18c',
'user_token' => '1448adg28aswJcXdUBiB09VD5T1Lvl6ggp2HYc5Q81whrF7p5I',
'user_secret' => 'dadffli224sZoh8VzLIQ2f6zZg0XPtx3nneUwwXs'
);
// Next pass this along with other info to the jc_twitter_info function
$my_twitter_feed = jc_twitter_info( 'clarktechnet', 5, 'feed', 2160, $twitter_auth);
$my_twitter_follower = jc_twitter_info( 'clarktechnet', 0, 'followers', 2160, $twitter_auth);
/**
* Twitter Info
*
* @param string $user user of twitter feed to retrieve.
* @param string $count number of tweets to retrive.
* @param string $type type of info to retrive.
* @param int $cache number of seconds transient is valid for.
* @param array $auth array of auth keys for signing the request
*
* Inspiration for code:
* Chip Bennet's oenology theme https://github.com/chipbennett/oenology and
* catswhocode http://www.catswhocode.com/blog/snippets/grab-tweets-from-twitter-feed
*
* @return string of formatted API data
*
* @since 1.0
*/
function jc_twitter_info( $user = 'clarktechnet', $count = '3', $type = 'feed', $cache = 2160, $auth = array( ) ) {
require_once get_template_directory() . '/inc/twitter/tmhOAuth.php';
require_once get_template_directory() . '/inc/twitter/tmhUtilities.php';
if ( empty( $auth ) ) {
return;
}
//create auth class
$tmhOAuth = new tmhOAuth( $auth );
if ( $type == 'feed' ) {
// Build Twitter api url
$remote = $tmhOAuth->request( 'GET', $tmhOAuth->url( '1.1/statuses/user_timeline' ), array(
'include_entities' => '1',
'screen_name' => $user,
'count' => $count,
) );
//cache request
$transient_key = "jc_" . $user . "_twitter";
} elseif ( $type == 'followers' ) {
// Build Twitter api url
$remote = $tmhOAuth->request( 'GET', $tmhOAuth->url( '1.1/users/show' ), array(
'screen_name' => $user
) );
//cache request
$transient_key = "jc_" . $user . "_twitter_follow";
}
$i = 1;
$cached = get_transient( $transient_key );
if ( false !== $cached ) {
return $cached . '<!--Returned from cache-->';
}
// If the API returns a server error in response, output
// an error message indicating the server response.
if ( '200' != $remote ) {
return '<p>' . __( 'Twitter unavailable.', 'slug' );
}
// If the API returns a valid response, the data will be
// json-encoded; so decode it.
$data = json_decode( $tmhOAuth->response['response'] );
if ( $type == 'feed' ) {
$output = "<ul>\r\n";
while ( $i <= $count ) {
//Assign feed to $feed
if ( isset( $data[$i - 1] ) ) {
$feed = jc_twitter_format( $data[$i - 1]->text, $data[$i - 1] );
$id_str = $data[$i - 1]->id_str;
$output .= "<li class='tweet'>" . $feed . " - <em>\r\n<a href='http://twitter.com/$user/status/$id_str'>" . human_time_diff( strtotime( $data[$i - 1]->created_at ), current_time( 'timestamp' ) ) . " " . __( 'ago', 'slug' ) . "</a></em></li>\r\n";
}
$i++;
}
$output .="</ul>";
} elseif ( $type == 'followers' ) {
$output = $data->followers_count . " " . __( 'followers', 'slug' );
}
set_transient( $transient_key, $output, $cache );
return $output;
}
/**
* Formats the tweet using the given entities element.
*
* @param string $raw_text just the text of the tweet
* @param object $tweet an array of the tweet including entites
* @return string the tweet text with entities replaced with hyperlinks
*
* source: http://dmblog.ca/2011/08/how-to-use-tweet-entities/
*
* @since 1.0
*/
function jc_twitter_format( $raw_text, $tweet = NULL ) {
// first set output to the value we received when calling this function
$output = $raw_text;
// create xhtml safe text (mostly to be safe of ampersands)
$output = htmlentities( html_entity_decode( $raw_text, ENT_NOQUOTES, 'UTF-8' ), ENT_NOQUOTES, 'UTF-8' );
// parse urls
if ( $tweet == NULL ) {
// for regular strings, just create <a> tags for each url
$pattern = '/([A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/i';
$replacement = '<a href="${1}" rel="external">${1}</a>';
$output = preg_replace( $pattern, $replacement, $output );
} else {
// for tweets, let's extract the urls from the entities object
foreach ( $tweet->entities->urls as $url ) {
$old_url = $url->url;
$expanded_url = ( empty( $url->expanded_url ) ) ? $url->url : $url->expanded_url;
$display_url = ( empty( $url->display_url ) ) ? $url->url : $url->display_url;
$replacement = '<a href="' . $expanded_url . '" rel="external">' . $display_url . '</a>';
$output = str_replace( $old_url, $replacement, $output );
}
// let's extract the hashtags from the entities object
foreach ( $tweet->entities->hashtags as $hashtags ) {
$hashtag = '#' . $hashtags->text;
$replacement = '<a href="http://twitter.com/search?q=%23' . $hashtags->text . '" rel="external">' . $hashtag . '</a>';
$output = str_ireplace( $hashtag, $replacement, $output );
}
// let's extract the usernames from the entities object
foreach ( $tweet->entities->user_mentions as $user_mentions ) {
$username = '@' . $user_mentions->screen_name;
$replacement = '<a href="http://twitter.com/' . $user_mentions->screen_name . '" rel="external" title="' . $user_mentions->name . ' on Twitter">' . $username . '</a>';
$output = str_ireplace( $username, $replacement, $output );
}
// if we have media attached, let's extract those from the entities as well
if ( isset( $tweet->entities->media ) ) {
foreach ( $tweet->entities->media as $media ) {
$old_url = $media->url;
$replacement = '<a href="' . $media->expanded_url . '" rel="external" class="twitter-media" data-media="' . $media->media_url . '">' . $media->display_url . '</a>';
$output = str_replace( $old_url, $replacement, $output );
}
}
}
return $output;
}
?>
@nedzen
nedzen commented Mar 24, 2015

is this twitter embed with a certain #hashtag ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment