Providing a retweet link on your site is a great way to encourage Twitter users to let others know about a post. In this tutorial I provide you with an easy way to do this in WordPress and then explain it for those who are interested. This tutorial is perfect for those who want to provide retweet links that use their own domain name for the short url.
Custom short url using La Petite Url
Before you can use the code I provide here you need to install the La Petite Url plugin to your WordPress installation. This is a beautiful plugin that creates short urls using your own domain name.
Retweet services such as tweetmeme are wonderful, but I moved away from using tweetmeme because I wanted to use my own domain for the link to preserve branding.
What the code does
My code takes a function from La Petite Url that would usually echo the shorturl and instead create a retweet link that is than echoed.
It also strips out www. if it exists in the url to make it even shorter. This is needed beause La Petite Url builds up the url using bloginfo('site_url') which will contain www. if that's how you have your normal urls display.
This hack is also safe to use in the event La Petite Url gets deactivated. If the La Petite Url function this is based on does not exist my function will switch to using the post's permalink, minus www., which Twitter will then convert to a bit.ly link if need be.
The code
Copy the following code into your theme's functions.php file.
Update - Thank you very much to Chris Neale for pointing out I was using preg_replace() where only str_replace() was needed. I have updated the code below.
/**
* Must be called within The Loop
*/
function retweet_url() {
$the_url;
//if "la petite url" plugin is available
if(function_exists('the_petite_url_link')) {
//an edit of the_petite_url_link() from the plugin
global $wp_query;
global $wpdb;
global $petite_table;
$blogurl = get_bloginfo('siteurl');
$url_table = $wpdb->prefix . $petite_table;
$post_id = $wp_query->post->ID;
$anchor_text = get_option('le_petite_url_link_text');
$petite_url = $wpdb->get_var("SELECT petite_url FROM ".$url_table
." WHERE post_id = ".$post_id."");
if($petite_url != "")
{
$le_petite_url_permalink = $blogurl;
if(get_option('le_petite_url_permalink_prefix') != "")
{
$le_petite_url_permalink = $le_petite_url_permalink
. get_option('le_petite_url_permalink_custom');
}
else
{
$le_petite_url_permalink = $le_petite_url_permalink . "/";
}
$le_petite_url_permalink = $le_petite_url_permalink . $petite_url;
//below differs from the_petite_url_link()
$the_url = $le_petite_url_permalink;
}
else {
$the_url = get_permalink();
}
}
else
$the_url = get_permalink();
//for accessing the post title directly
global $post;
//edit this to your Twitter name
$twitter_account = "YOURSITE";
echo "http://twitter.com/home/?status=RT+%40"
.$twitter_account."+"
.urlencode($post->post_title)."+"
.str_replace("www.", "", $the_url);
}
Don't forget to edit the $twitter_name variable!
Using it in context
You can now simply call retweet_url() in your theme where you want the url. You must call it within the loop.
Here is an example of how I use it on Arteki:
<a title="Tweet about this article!" href="<?php retweet_url(); ?>" rel="nofollow">Tweet</a>
This is the basis of my retweet buttons, which you can see below this article in the "Share this" section.
The code explained
For those that are interested I will explain the parts of the code that builds the retweet url.
$the_url = get_permalink();
get_permalink() is a WordPress function that is essentially the same as the_permalink(). The difference is that it returns the permalink rather than echoing it.
global $post;
.urlencode($post->post_title)."+"
global $post brings into scope the array that contains all the information about the post in the current loop of The Loop. This is needed because otherwise $post would refer to a new local variable of the same name. From this we can grab the post's title then encode it using urlencode to make special characters their url friendly versions.
It would of course be possible to use get_the_title() to retrieve the title of the post to encode, but this leads to some issues when using urlencode. It seems WordPress functions will convert any special characters such as apostrophes into their number code equivalent, e.g. ' for an apostrophe. urlencode then converts all of those characters to their url friendly versions. This causes an ugly string of characters to be in the Twitter status box instead of the desired character.
I may be wrong about the exact problem between the_title() and urlencode() but this workaround was needed.
.str_replace("www.", "", $the_url);
This is what removes www. from the url to make the url shorter.
Your thoughts or questions
Do you have any thoughts or questions on this solution? I'd love to hear from you so please leave a comment, or if you'd rather you can email me using the contact form.
I'd also be interested to hear of any existing WordPress retweet links, e.g. code you've seen or came up with or even plugins.
Post info
Short URL: http://arteki.com/mcxtr
Categories: Tutorials


Subscribe by RSS
Subscribe by email
Have your say
7 comments
Thats really strange.. Ive not experienced anything like that.
Does the ‘www.’ replacement really need a regular expression? str_replace() would do, wouldn’t it?
You are quite right, thank you for pointing this out to me. In fact I’ve just noticed my regex is wrong anyway – I missed a backslash before the dot so it would match www followed by any character. I’ll correct it to use str_replace()
Can you direct me to something almost the opposite? I’m trying to find a sys/plug-in that will allow me to publish specific tweet streams, not only my own, but other authors RSS into posts. Having no luck so far. Plenty of sidebar tweet RSS widgets but no template main column RSS twitter plugins.
Any help
Thanks in advance for the time and effort.
Stuart ONeill
stuart.j.oneill@gmail.com
Hi there. What you’re after isn’t something I’m familiar with, but I’ve done a bit of searching and found what is probably the best solution for what you’re after.
* WP-o-Matic – a RSS agreggator plugin, which is probably just what you need. You could also you it for publishing tweets via the RSS feed for a user (available on the sidebar of their twitter profile page).
A word of caution: both tweets and contents of RSS feeds are the intellectual property of the person who originally wrote the content, so you may run into some legal issues if posting up from RSS feeds without permission. If you credit your source you should be okay as this satisfies most publishers, however it is not strictly legal (depending on what the source says about re-posting of the content and attribution).
I’m going to email this reply to you too to make sure you get this info
I hope it helps.
Thanks for stopping by!
Who does your Webmaster and SEO work? Nice Job!
I do it all myself.
Thanks for your comment (and the other one too), though I know they were probably spam. You seemed to put some effort into the text in them though so I’ve let them past the filter.
Trackbacks / Pingbacks
Trackback URL: http://www.arteki.com/how-to-make-custom-retweet-links-in-wordpress/trackback/
None.