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 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() {
    //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');
                $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();
        $the_url = get_permalink();
    //for accessing the post title directly
    global $post;
    //edit this to your Twitter name
    $twitter_account = "YOURSITE";
    echo ""
    .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;

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. &#39; 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.

Share this

    Have your say

    Leave a Reply


    Trackbacks / Pingbacks

    Trackback URL: