Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Last active October 4, 2018 15:56
Show Gist options
  • Save nfsarmento/ea2e4e37ba75740353796509878a9a9f to your computer and use it in GitHub Desktop.
Save nfsarmento/ea2e4e37ba75740353796509878a9a9f to your computer and use it in GitHub Desktop.
WordPress Twitter Feed - TwitterOauth
<?php
/* REQUIRES: TwitterOAuth
* https://github.com/abraham/twitteroauth/tree/master/twitteroauth
*
* Download and place in a /twitteroauth/ folder in your theme/plugin.
*/
session_start();
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$numTweets = 3; // Number of tweets to display.
$name = 'XXXXXX'; // Username to display tweets from.
$excludeReplies = true; // Leave out @replies
$transName = 'list-tweets'; // Name of value in database.
$cacheTime = 1; // Time in minutes between updates.
$backupName = $transName . '-backup';
// Do we already have saved tweet data? If not, lets get it.
if(false === ($tweets = get_transient($transName) ) ) :
// Get the tweets from Twitter.
$connection = new TwitterOAuth(
'XXXXXXXXXXXXXXXXXXXX', // Consumer key
'XXXXXXXXXXXXXXXXXXXX', // Consumer secret
$access_token,
$access_token_secret
);
$content = $connection->get("account/verify_credentials");
// If excluding replies, we need to fetch more than requested as the
// total is fetched first, and then replies removed.
$totalToFetch = ($excludeReplies) ? max(3, $numTweets * 1) : $numTweets;
$fetchedTweets = $connection->get(
'statuses/user_timeline',
array(
'screen_name' => $name,
'count' => $totalToFetch,
'exclude_replies' => $excludeReplies
)
);
// Did the fetch fail?
if($connection->http_code != 200) :
$tweets = get_option($backupName); // False if there has never been data saved.
else :
// Fetch succeeded.
// Now update the array to store just what we need.
// (Done here instead of PHP doing this for every page load)
$limitToDisplay = min($numTweets, count($fetchedTweets));
for($i = 0; $i < $limitToDisplay; $i++) :
$tweet = $fetchedTweets[$i];
// Core info.
$name = $tweet->user->name;
$permalink = 'http://twitter.com/'. $name .'/status/'. $tweet->id_str;
/* Alternative image sizes method: http://dev.twitter.com/doc/get/users/profile_image/:screen_name */
$image = $tweet->user->profile_image_url;
// Message. Convert links to real links.
$pattern = '/http:(\S)+/';
$replace = '<a href="${0}" target="_blank" rel="nofollow">${0}</a>';
$text = preg_replace($pattern, $replace, $tweet->text);
//$text = $tweet->text;
// Need to get time in Unix format.
$time = $tweet->created_at;
$time = date_parse($time);
$uTime = mktime($time['hour'], $time['minute'], $time['second'], $time['month'], $time['day'], $time['year']);
// Now make the new array.
$tweets[] = array(
'text' => $text,
'name' => $name,
'permalink' => $permalink,
'image' => $image,
'time' => $uTime
);
endfor;
// Save our new transient, and update the backup.
set_transient($transName, $tweets, 1 * $cacheTime);
update_option($backupName, $tweets);
endif;
endif;
// Use delete_transient if you want to test it
// delete_transient($transName);
// Now display the tweets.
?>
<div id="tweets">
<?php foreach($tweets as $t) : ?>
<p>
<?php echo $t['text']; ?>
</p>
<!--
<div class="twitter_intents">
<p><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$t['id_str'].'">Reply</a></p>
<p><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$t['id_str'].'">Retweet</a></p>
<p><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$t['id_str'].'">Favorite</a></p>
</div>';
-->
<?php endforeach; ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment