Adding a Twitter Feed To Your Site

With CF8 and the cffeed it’s easy to add a feed of your tweets to your site. I’ve found this is a really good way of getting followers on Twitter. I’ve used cfpod and cffeed below

I like to style my cfpod’s but IE (at least to IE7 and as per usual)  has “issues” so i just do a browser sniff and use a couple of variables to make the pod look the same in all browsers

<CFIF FindNoCase("msie", CGI.HTTP_USER_AGENT, "1")>
 <CFSET podstyle = "color:##000000;text-align:center">
 <CFSET podHT = "75">
 <CFELSE>
 <CFSET podstyle = "color:##FFFFCC;background-color:##6699CC;text-align:center">
 <CFSET podHT = "65">
 </CFIF>
 <cfpod headerStyle="#podStyle#"   name="twitpod" height="#podHT#" width="290" title="Me On Twitter - Latest">
 <div style="font-weight:normal;">
 <cfset feedurl="http://search.twitter.com/search.atom?q=yourTwitterName -@yourTwitterName" />
 <cffeed source="#feedurl#" properties="feedmeta" query="feeditems" />
 <cfoutput query="feeditems" maxrows="1">
 <span style="font-size:.7em;margin-top:-5px;">
 #dateformat(listfirst(feeditems.publisheddate,"T"), "mm/dd/yy :hh:mm")#
 </span>
 <span style="font-size:.8em;">
 #REReplaceNoCase(feeditems.Content, "<[^>]*>", "", "All")#<br>
 </span>
 </cfoutput>
 <a href = "http://twitter.com/yourTwitterName" style="text-decoration:underline;" target="_blank">Subscribe To My Twitter Feed</a>
 </div>
 </cfpod>

My twitter account is a corporate presence so I don’t want other people’s tweets showing on my site.  If you were just to set your feed url to

http://search.twitter.com/search.atom?q=yourTwitterName

you would get all hits for yourTwitterName, even those posts from others who are @replying to you.  This might not be an issue for some I want to make sure only -my- tweets show up in the feed. Just add

-@yourTwitterName

to the search.atom criteria and this will exclude any @yourTwitterName search results

One other thing you may notice is that I’ve added a html stripping rereplace funciton.

#REReplaceNoCase(feeditems.Content, “<[^>]*>”, “”, “All”)#

This is a good idea for feeds and any source for which you have no control over. I don’t want visitors to my site to have any issues with potential attacks propagated through Twitter (or any other service). We’ve already seen a couple of Twitter “worms” and I don’t need a potential XSS vulnerability introduced on my site. (paranoid..yes but that’s a -good- thing)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: