For another site of mine I needed a way to display the latest Tweet in the header. Sure, there are probably hundreds of Twitter plugins for WordPress. And there’s also code all over the internet to do the same. But what I couldn’t find was one that cached.
Without caching each time the page was viewed my server would have had to hit Twitter and parse out the JSON; not a good solution. So this little plugin caches the request into the options table and only refreshes when needed. By default it’s every 5 minutes, but you can change that.
Usage
Upload the plugin to your wp-content/plugins folder and activate.
Insert <?php just_a_tweet(‘username’); ?> into your theme where you’d like it to show up.
The just_a_tweet takes 4 parameters.
$twitterUser – Required – The Twitter user name of the account you want to use
$cacheAge – Optional – The number of minutes to wait until the cache is considered stale. Defaults to 5
$forceRefresh – Optional – If true, then the caching is not done. Defaults to false
$echo – Optional = If true the tweet is echoed. The function returns the tweet either way.
Download
Download from the WordPress plugin library
#1 by Wouter on May 18, 2011 - 3:54 am
This is exactly what I was looking for, thanks! Is it possible to use this for multiple usernames? So on one page display tweets from user X and on another page of my website display tweets from user Y?
#2 by Ryan on May 18, 2011 - 5:21 am
Sure, just switch the username parameter.
#3 by Bill on May 18, 2011 - 4:46 pm
I’m obviously doing something wrong. I get “Error: just_a_tweet could not retrieve from Twitter” mean?
I added my Twitter account name to the template code you require, and tried it with both the ampersand and without. With breaks the site. Without returns the Error.
What am I missing?
#4 by Ryan on May 18, 2011 - 6:32 pm
Let’s say your Twitter username is Bill, then you’d type in
<?php just_a_tweet(‘bill’); ?>
You can also try going to http://twitter.com/statuses/user_timeline.json?screen_name=bill&count=1 and make sure that Twitter is returning the right thing.
Of course replace “bill” with whatever your Twitter username really is.
#5 by Maes on June 7, 2011 - 6:32 am
I have a problem with the length of the tweet, I tried to control(.jat_wrapper ) But it didn’t work. You know a way?
#6 by Ryan on July 21, 2011 - 6:31 am
Length like the length of the tweet? The plugin just dumps whatever gets returned by Twitter.
Is that not what you’re asking?
#7 by Chaim on July 21, 2011 - 12:02 am
Thanks for a nice clean solution to something simple. Just as a tip for anyone who wants to use it as short code, just put this at the bottom of the code in the plugin editor (you can change the default to whatever you’d like):
function just_a_tweet_func($atts) {
extract(shortcode_atts(array(‘twitteruser’ => ‘just_a_tweet’), $atts));
return just_a_tweet($twitteruser, 5, false, false);
}
add_shortcode( ‘just_a_tweet’, ‘just_a_tweet_func’ );
#8 by Chaim on July 21, 2011 - 12:03 am
And then you can do [just_a_tweet twitteruser=some_twittter_user] and it will display the most recent tweet from that user anywhere inline.
#9 by Ryan on July 21, 2011 - 6:32 am
Very cool. When I wrote this it was for the header in a theme so a short code didn’t even cross my mind. May have to add this in and push out a new version.
#10 by Ryan on July 23, 2011 - 10:41 am
Just committed a version 0.2 of this plugin with the short code stuff added. Thanks for the suggestion.
#11 by non_descript on August 11, 2011 - 4:59 am
Hi,
This plugin offers the exact functionality that I need for my site but I have a problem with it. When I add to my page and run it I get the following error message:
“Fatal error: Cannot use string offset as an array in C:\Program Files (x86)\VertrigoServ\www\wp-content\plugins\just-a-tweet\just-a-tweet.php on line 43″
Any suggestions why I get this and how it can be fixed?
Thanks!