How to use short URLs for your Twitter share button

One of the only issues that I’ve ever had with social sharing plug-in Digg Digg was the fact that it didn’t shorten the URL on the Tweet button. When people would tweet from my website, it always looked a little unrefined to me. And since Twitter’s URL shortener only reveals on mobile Twitter apps, anyone seeing my link on their desktop would see my full URL in all its clunky splendiferousness. I decided I needed to figure it out and I did.

There are two steps for creating this functionality for your Twitter share button:

  • Change the WordPress short URL to using Shortlinks plug-in
  • Create a Twitter button with a PHP call for the shortlink

Change the WordPress short URL to

I’m sure that there’s a way to do this without a plug-in, but I’m lazy. I downloaded the shortlinks plug-in, which converts the WordPress short URL to a URL. It takes a minimal amount of configuration and the instructions are pretty straightforward. Note that it doesn’t change all of your URLs at once: saving a post triggers the action.

Create a Twitter button with a PHP call for the shortlink

(Note that I changed all of my share buttons to manual buttons, but I imagine you could do this with Digg Digg or any other social sharing plug-in by finding the Tweet button in the code) 

On the Twitter buttons page, Twitter will create a generic code which shares the URL of any page you put it on. It looks something like this:

<a href=”” class=”twitter-share-button” data-via=”leaderswest”>Tweet</a>

We’re going to ignore the script and focus on the “a” link. We’re going to add an attribute “data-url” that ties back to the shortlink that the plug-in created. Like this:

<a href=”” class=”twitter-share-button” data-url=”php echo wp_get_shortlink(); ?> data-via=”leaderswest”>Tweet</a>

We’re not finished, though: One of the peculiarities about implementing this is that all of the previous share data disappears. So, we’re going to add one more attribute (data-counturl) to tell Twitter what the short-url links to. We do that with a PHP call back to the permalink, like so:

<a href=”” class=”twitter-share-button” data-url=”<?php echo wp_get_shortlink(); ?> data-counturl=”<?php echo get_permalink(); ?> data-via=”leaderswest”>Tweet</a>

Now you have a short-url in your Tweets, the counters are fully functional, and that’s all there is to it. Share this post and check it out. :)


Photo by Igor Bredikhin (Own work) [GFDL or CC-BY-3.0], via Wikimedia Commons
Jim Dougherty

Jim Dougherty

Writer and chief of miscellany at
I'm the guy that wrote the article you just read. Sorry for the typos.

    Hello to all, how is everything, I think every one is getting more from this site, and
    your views are good in support of new people.