UPDATE: Pat from AddToAny describes a better way to do this in the comments. I’ve written an updated article describing the new process.
I’ve been playing around with Yourls and it’s accompanying Wordpress Plugin as a personal link shortening and sharing service for Twitter. So far it’s been very handy. I also recently added a “Share/Save This Entry” widget to the bottom of my blog posts courtesy of the AddToAny WordPress Plugin. I noticed that AddToAny was using the full URL for the posts and wondered how hard it would be to get it to use my custom shortened urls instead. Turns out, it’s really easy as long as you have both of these plugins installed.
A word of warning… this involved a (very minor) edit to the AddToAny plugin’s source code. That means that it’ll most likely break if and when you update the AddToAny plugin.
Find this block of code in wp-content/plugins/add-to-any/add-to-any.php:
function A2A_SHARE_SAVE_link_vars() {
global $post;
$linkname = get_the_title($post->ID);
$linkname_enc = rawurlencode( $linkname );
$linkurl = get_permalink($post->ID);
$linkurl_enc = rawurlencode( $linkurl );
return compact( 'linkname', 'linkname_enc', 'linkurl', 'linkurl_enc' );
}
… and replace get_permalink($post->ID) with wp_ozh_yourls_geturl($post->ID), like so:
function A2A_SHARE_SAVE_link_vars() {
global $post;
$linkname = get_the_title($post->ID);
$linkname_enc = rawurlencode( $linkname );
$linkurl = wp_ozh_yourls_geturl($post->ID);
$linkurl_enc = rawurlencode( $linkurl );
return compact( 'linkname', 'linkname_enc', 'linkurl', 'linkurl_enc' );
}
All this does is replace the standard “get_permalink” call with a call from the Yourls WordPress plugin that’ll return the shortened url instead. If one doesn’t exist, it’ll create it first. Very handy and very easy.
Although this seems to work so far, I found a problem where AddToAny has a really lame “feature” that shortens urls and adds their own attribution to the end of the generated tweet. So, after hitting the “Send to Twitter” button from a post on my site, you end up with something like this:
HTML5 Enabling JavaScript for Internet Explorer http://bit.ly/666Gd via @AddToAny
What? Really? I’m not sure how they feel they can add their attribution to a link that is simply shared through their service and not discovered through their service, but that’s besides the point.
I googled around and even asked them via Twitter, but couldn’t find a way to disable this “feature.”
Hmm… now if only AddToAny didn’t automatically shorten my already shortened url on Twitter… Luckily, you can define custom services within the “Additional Options” section of the AddToAny Share/Save settings from within WordPress.
So, I added another service for Twitter:
a2a_custom_services = [
["Twitter",
"http://twitter.com/home?status=A2A_LINKNAME_ENC%3A%20A2A_LINKURL_ENC",
"http://example.com/wp-content/plugins/add-to-any/icons/twitter.png"
]
];
Be sure to change the url for the twitter icon as I’m just using an example. It looks something like this in the admin panel:

This allows you to keep the shorturl provided by Yourls. An added benefit is that it doesn’t go through AddToAny’s service proxy first. The only problem now is that you’ll have two Twitter buttons on the AddToAny popup:

So, you’ll also need to add this CSS style declaration to your page to hide their existing Twitter service from the widget:
#a2apage_twitter.a2a_sss {
display: none;
}
This is what you’ll end up with:

This is obviously not an optimal solution for a number of reasons. Firstly, it means you’ll end up having an odd number of items in the share overlay. I tried changing the number of services listed using one of the built-in options but it didn’t have any effect. (Most likely because I’m messing with their CSS directly.) Also, the custom service won’t take advantage of the smart menus where visitors services are displayed first and in bold. In the end though, I’m willing to sacrifice that for the sake of having my own attribution and short links.
Is there an easier way to do this? If there is, I’m all ears.