Listing Multiple Twitter Accounts On Your Website.
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 and its all free. 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 service 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. That way anyone visiting their website can follow the activities of the day – real time. Each crew leader has his own twitter account so there will be multiple tweets from multiple users coming into the website. I think this is an amazing application of social media to mainstream business. It has followers written all over with good solid information being posted. People will read the feeds on twitter and see them on the site and instantly know these people are experts at what they are doing. It also has that real world thing going on. People like that – they connect with that.
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 in his blog 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.