I have been building websites for some friends and clients since I learned how easy it is to set up WordPress sites. It is amazing how many applications are available as plug-ins and widgets. You can add practically anything you need to your site as a plug-in. So when a client asked me if it would be possible to display multiple twitter accounts to his site I said, “of course we can”. Well, I soon learned that I had violated the assumption rule.
First, I will tell you about the application because I think its pretty cool. The site is for one of the largest concrete pouring companies in Atlanta. Their crews are on multiple sites on a daily basis and they want to have each crew leader submit tweets throughout the day from their phone with pictures and info about what they are doing. Each crew leader has his own twitter account so there will be multiple tweets from multiple users coming into the website.
When I started to search for this application I thought it would be pretty easy to find since I knew there were plug-ins for bringing twitter feeds into your site. I found many different plug-ins for adding single account twitter feeds to your site but I could not find anything to accomplish what I was trying to do. After spending a couple of days looking for this particular application I finally found something that seemed promising. As I looked deeper into the listing I had found with my gazillionth Google search there seemed a glimmer of hope. I was a little skeptical after being led down many dead ends over the last couple of days. Unbelievably, after some investigation it still smelled good. It looked good - Hmmm – just maybe this would work. Amazing. After countless searches I was looking at a page of code that was saying it was going to do exactly what I was looking for. It wasn’t a plug-in though, just a page of code. It had been written by a young man in Colorado named Ryan Barr.
Ryan had written a three part series of projects to bring twitter feeds into a site. Part one was about pulling a single twitter users last tweet into a site. Part two was about pulling multiple tweets from a single user into a site. And part three was dead on about what I was looking for. Thank God, the search was over. I was pretty amazed that it had taken me this long to find this application. There were some things I had found along the way that sounded like it was going to do what I needed but after further investigation they all just worked with one twitter account.
It took me a little bit of time to figure out how I was going to get Ryan’s code to work on my site. What I ended up doing was hard coding Ryan’s php code into the sidebar of the theme I was working with. It worked like a charm.
Here is the code Ryan wrote to make it happen. You can change the variables at the top to meet your needs. There is a description and an example below the code to detail how to change the variables. Ryan was very thorough and attentive to making sure his program was as user friendly as possible.
<?php
// Pull from which accounts? Separated by a space, for example: Username Username Username
$usernames = "Username Username Username";
// Number of tweets to pull in, total.
$limit = "5";
// Show username? 0 = No, 1 = Yes.
$show = 1;
// REMEBER: When using HTML, escape double-quotations like this: \"
// This comes before the entire block of tweets.
$prefix = "";
// This comes before each tweet on the feed.
$prefix_sub = "";
// This comes after the username but before the tweet content.
$wedge = "";
// This comes after each tweet on the feed.
$suffix_sub = "";
// This comes after the entire block of tweets.
$suffix = "";
// It is recommended that you do not modify below this point without PHP knowledge.
function parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub) {
$usernames = str_replace(" ", "+OR+from%3A", $usernames);
$feed = "http://search.twitter.com/search.atom?q=from%3A" . $usernames . "&rpp=" . $limit;
$feed = file_get_contents($feed);
$feed = str_replace("&", "&", $feed);
$feed = str_replace("<", "<", $feed);
$feed = str_replace(">", ">", $feed);
$clean = explode("<entry>", $feed);
$amount = count($clean) - 1;
for ($i = 1; $i <= $amount; $i++) {
$entry_close = explode("</entry>", $clean[$i]);
$clean_content_1 = explode("<content type=\"html\">", $entry_close[0]);
$clean_content = explode("</content>", $clean_content_1[1]);
$clean_name_2 = explode("<name>", $entry_close[0]);
$clean_name_1 = explode("(", $clean_name_2[1]);
$clean_name = explode(")</name>", $clean_name_1[1]);
$clean_uri_1 = explode("<uri>", $entry_close[0]);
$clean_uri = explode("</uri>", $clean_uri_1[1]);
echo $prefix_sub;
if ($show == 1) { echo "<a href=\"" . $clean_uri[0] . "\">" . $clean_name[0] . "</a>" . $wedge; }
echo $clean_content[0];
echo $suffix_sub;
}
}
echo $prefix;
parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub);
echo $suffix;
// WRITTEN BY RYAN BARR (SPOOKY)! SPOOKYISMY.NAME
?>
If you notice, the upper half of the script contains a few variables (each with a description of what each one does) that will modify how your Twitter block is displayed. Here are the eight variables that can be changed: $usernames, $limit, $show, $prefix, $prefix_sub, $wedge, $suffix_sub, $suffix. Let’s go over what each one does:
- $usernames – Which username’s the script will pull tweets from, separated by spaces. (" ")
- $limit – How many tweets for the script to pull. (Twitter set the maximum to 100 tweets.)
- $show – Whether or not to display the user names with the tweets.
- $prefix – What’s to be displayed before the block of tweets.
- $prefix_sub – What’s to be displayed before each individual tweet.
- $wedge – What’s to be displayed after each user name. (If $show is set to 1.)
- $suffix_sub – What’s to be displayed after each individual tweet.
- $suffix – What’s to be displayed after the block of tweets.
$usernames = "ryanbarr codyfisher";
$limit = "5";
$show = 1;
$prefix = "<h1>Twitter Feed</h1><ul>";
$prefix_sub = "<li>";
$wedge = " says: ";
$suffix_sub = "</li>";
$suffix = "</ul><p><a href=\"#\">More from our Twitter feeds!</a></p>";
I have twittered with Ryan about his work and he says he is working on a plug-in to be released shortly. I think it will be a great application addition to the plug-in arsenal of WordPress. As twitter continues to grow in popularity and people activate multiple accounts there will certainly be more of these application needs. Thanks Ryan for providing this for us and I look forward to using your plug-in when it comes out. If you want to Visit Ryan’s Blog he has some great stuff in addition to this gem. You can also Follow Ryan On Twitter. I think he is someone to watch and learn from.
In the meantime, if you have any questions or comments about the application of this awesome tool please drop a note below.
Happy Blogging.
Lovin’ this code. Been looking everywhere for something like this and have it working for the most part. A couple things though… Why do links within posts show up as HTML links with href, etc? And how would I go about displaying the picture of each twitter user? Also, how do I display the time that the tweet was posted?
Any thoughts?
Logan,
Glad I could help you out. I searched for days until I found this code as well. That’s one of the reasons I did a post about it because I knew other people must be looking for this as well. I would suggest going to Ryan’s site or leaving him a tweet. You can find those links in the post. He is the author of the code and although I have some coding skills – PHP is not my strong suit. Best of luck and let me know if I can help you in any other way. By the way – what is your application you are using the code for and how are you inserting the code into your site. I would be interested and it is great info for other people.
Logan, did you find an answer to your questions about the HTML links and time of tweet post? I would be very interested in that too.
Todd, thanks for sharing the article. I used it for my PHP-Fusion site by making a .PHP file as an infusion panel, then included it. I also made a few HTML-wise edits. Great article, however!
This is great! Has anyone implemented this and can you please please post your website address. I’d also like to see if anyone has done this in another language besides php…like javascript. I’d love to figure out how to pull in random tweets for an idea I have!
Thanks Todd and Thanks to Ryan, send him my regards! I’m on twitter too. I’ll follow Ryan pronto. Please update as you see some implementations of this!
In case anybody still likes to use this script; I’ve got a working example. Fixed HTML and showing the time of the tweet. Thanks to Ryan Barr.
Can you show it? Thanks!
Todd,
Thanks for posting this. I am building a website or a non-profit radio station in Austin, TX called The Source. http://thesource.fm I am going to give this a try in lieu of a web 1.0 chat forum. My hope is that DJs can each use their own Twitter account to connect with their fans. This may be the solution. Thanks again.
Jimi Jean
Hi, thanks for this, just what I was after.
One note, to make the HTML links show correctly instead of ‘<a href’ just change the line that has:
echo $clean_content[0];
to
echo html_entity_decode($clean_content[0]);
Dave
Thanks Todd perfect fix! Great piece of code, amazed that a client required this and the exact script was out there…
Thank you, Todd!
Your article is nicely written and Ryan’s sample code is really perfect for beginners on PHP, like me. I also sent Ryan a thank-you note on Twitter.
Thanks also to Dave for sharing the fix using the html_entity_decode function.
My application is a page in joomla/php. The tweets shown by the page are written in Brazilian Portuguese and contain special chars, so I had use the utf8_encode function on top of html_entity_decode:
utf8_encode(html_entity_decode($clean_content[0]));
this is awesome! except for some reason the tweets aren’t … -ing can someone let me know where to place those pesky little s so that the posts will be
USER
Post
thanks!
weird .. it didnt show the most important part of the post
i’m looking for where to add the BR (as in linebreak)
sorry for triple posting, but i figured out the line break thing… now i’m wondering if it’s possible to time stamp each post?
furthermore … i saw a code for timestamping as “time ago” but i’m sorta new to php, could someone help me?
http://www.phpsnippets.info/display-dates-as-time-ago
Can someone do a tutorial on how to add timestamp functionality please? Great script, otherwise!