Search

I tweeted this weekend about creating a "Tweet This" button using Symphony and the bit.ly API, and Anders suggested I post the solution here.

It's fairly straightforward. The first thing to know is the URL for posting to Twitter:

 http://twitter.com/home/?status=

When a user follows that link, whatever argument is passed to the status parameter will be pre-loaded into her/his status field (assuming they're logged in to Twitter). The most common use of this is, of course, to pass a URL.

The difficulty then is to keep the URLs as short as possible because of Twitter's 140-character limit. What we want, on the Symphony end, is to auto-generate shortened URLs for any entries/pages on which our "Tweet This" button will appear. Luckily, URL-shortening service bit.ly has a pretty simple XML API, and Symphony makes pulling in external XML a breeze.

Step One: Get a bit.ly API Account

Register for bit.ly. Once you've registered, log in and go to your account page. You'll see your API login and API Key in the left sidebar. Copy them.

Step Two: Create the Short URL Datasource

Create a new datasource (mine is called Short URL) with source set to Dynamic XML, and the following URL (replace the two all-caps strings in square brackets with your API Login and API Key):

 http://api.bit.ly/shorten?version=2.0.1&login=[YOUR-API-LOGIN]&apiKey=[YOUR-API-KEY]&longUrl={$current-url}&format=xml

Step Three: Attach the Datasource

Attach the datasource to any page(s) on which you want to have a "Tweet This" button. Because it uses the {$current-url} param, it doesn't matter if your page is static or accepts lots of parameters for displaying entries. You'll get a unique shortened URL for every possible URL on that page.

Step Four: Build Your Link

In your XSL templates, build the Tweet This link:

 <a href="http://twitter.com/home/?status={//shortUrl}">Tweet this</a>

Of course, you can always add some other elements since the URLs themselves are shortened:

 <a href="http://twitter.com/home/?status={entry/title}: {//shortUrl}">Tweet this</a>

I wrap mine in an xsl:if too, just to be safe. If I've forgotten to attach the DS or the bit.ly API isn't available, the link won't display:

 <xsl:if test="//shortUrl"><a href="http://twitter.com/home/?status={//shortUrl}">Tweet this</a></xsl:if>

That's all. Like I said, fairly straightforward but hopefully someone will find it useful. And feel free to suggest improvements here...

Great idea. I'll have to finish my Twitter extension soon. These to methods would work well together.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details