Simple Share on Twitter Social Link

The contents of this article may be out of date. It has been archived and will no longer be updated, comments are closed and the page is provided for reference purposes only.

At the moment it seems to be fashionable to add social networking links to your website. Recently a client wanted a quick and simple link to share a post on Twitter. The Twitter API is comprehensive but can be daunting for those with less advanced coding skills.

We can skip the complexities for the API and use a simple string in a URL to create our link. The code is straight forward enough:

<a href="http://twitter.com/home?status=Currently reading http://www.domain.com/post-url" title="Click to share this post on Twitter">Share on Twitter</a>

Test the link above to share this post on Twitter.

If you’re using a CMS and want to automatically use the posts permalink you can do so, I have included the code for a few popular CMS. We use the PHP function rawurlencode to make sure spaces in your string are replaced with it’s encoded equivalent.

Wordpress

<a href="http://twitter.com/home?status=<?php rawurlencode('Currently reading ' . the_permalink(false)); ?>" title="Click to share this post on Twitte"”>Share on Twitter</a>

Textpattern

<a href="http://twitter.com/home?status=<txp:php>rawurlencode('Currently reading ');</txp:php&gt:<txp:permlink />" title="Click to share this post on Twitter">Share on Twitter</a>

ExpressionEngine

<a href="http://twitter.com/home?status=<?php rawurlencode('Currently reading '); ?>{permalink="weblog/archives"}" title="Click to share this post on Twitter">Share on Twitter</a>

In this example we specify a template group/template with the entry ID that will automatically be added. You could also simply use {permalink} without specifying a template depending on your setup.

This article was posted on 25 June 2010 in Code, ExpressionEngine, Textpattern

That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.