Click here to download Tweet This v1.1.1
A plugin that adds a Twitter icon to every post and page, so your readers can share your blog entries on their Twitter accounts with ease. Shortens URLs in advance. Tested in WordPress 2.6.2, 2.0.11, and 1.5.
The Long Description
Adds a Post to Twitter bird icon to the top-right of every post and page, with a nice rollover effect. Shortens URLs in advance through Th8.us, eating up only 16 of 140 characters. Includes the post’s title after the link (can be turned off). If your titles are really long, they get cut off near 130 characters with “…”. Customize or disable auto-insertion under Settings > Tweet This.
Normally, posting a link to Twitter takes up a lot of space. Though they shorten URLs with TinyURL, it doesn’t happen till after you post your tweet, so the length of the original URL takes away from your 140 characters.
While your readers might go to TinyURL.com, copy and paste the blog post’s permalink, shorten the URL, copy that new URL, go to Twitter, and paste it into the box, this plugin merges all that into one step, using the Twitter API and Th8.us, an external URL trimming service I created. I didn’t like how long TinyURL’s URLs are, so I made Th8.us. Its URLs are 16 characters instead of TinyURL’s 25, giving you more space to write about the blog post. I use the www. prefix instead of http://, which saves three characters. Twitter picks up on it fine.
This plugin fetches a shortened URL from Th8.us for each of your blog posts’ permalinks server-side, then displays a link to Twitter for each post, with a cute icon. This is done automatically for each post as needed, but the shortened URLs are permanently cached (as a custom field in the postmeta table) to keep load times fast. The cached records are updated or deleted as needed when you edit a post’s permalink, delete a post, change your site’s permalink structure, or change URL services.
You can pick Th8.us, is.gd, urlb.at, bit.ly, Snurl, or TinyURL as your short URL service. The intrusive bird icon is the default, but you can also select a discrete “Tweet This” link in the settings, which is displayed at the bottom of each post.
Tweet This is extensible: you can disable the default icon under Settings > Tweet This, and then incorporate these functions into your theme within the loop:
<?php tweet_this_text_link(); ?> : Simple implementation. Echoes a text link to Twitter which says “Tweet This”.
<?php tweet_this_small(); ?> : Same as above, but with 16×16 Twitter “t” icon. Same output as checking “Automatically insert a small link at the end of posts instead of a large icon at the top-right” in the settings.
<?php tweet_this_url(); ?> : Echoes the Tweet This URL. An example: Share on Twitter!
<?php tweet_this_short_url(); ?> : Just echoes the short URL for the post (Th8.us, TinyURL, etc.), cached if possible.
<?php tweet_this_trim_title(); ?> : If “Include post title in tweet text” is checked, URL-encodes get_the_title(), truncates it at the nearest word if it’s overly long, and echoes.
You can prefix the last three functions above with get_ to return the data without echoing, for further manipulation by PHP.
Copyright 2008 Richard X. Thripp (email : richardxthripp@thripp.com)
Freely released under Version 2 of the GNU General Public License as published
by the Free Software Foundation, or, at your option, any later version.
Installation
1. Upload the `tweet-this` folder to `/wp-content/plugins/`.
2. Activate the plugin through the ‘Plugins’ menu in WordPress.
3. If you’re using WordPress MU and want this plugin active for all blogs, move `tweet-this.php` to `/wp-content/mu-plugins/` at this point.
4. Tweet This icons should automatically appear on every post and page! Go to Settings > Tweet This to change stuff.
5. Optionally, delete readme.txt and the screenshots from the tweet-this folder to save space on your server.
Upgrading from v1.0 to v1.1
You can just upload tweet-this.php, tweet-this.png (the logo is changed; I cleaned it up), and tweet-this-small.png (new file) to the tweet-this folder. No need to deactivate first.
Upgrading from v1.1 to v1.1.1
Just overwrite tweet-this.php with the new file.
Version History
1.1.1: 2008-09-11: I fixed an oversight from 1.1, so tweet_this_small() now works properly. If you stick with 1.1, you have to use echo tweet_this_small() instead to get the link plus icon to show up in your theme.
1.1: 2008-09-11: Added a ton of stuff. Short URLs are cached now, you can disable automatic insertion, tweet text includes post title, and there are configuration options. Also, the icon is 24-bit color. This bumps the file size up (10KB from 4KB), but it looks much cleaner.
1.0: 2008-09-01: First release, simple plugin with no config options.
Planned Features
• Internationalization support.
• Add a custom field “tweet_this_hide” to a post or page to have Tweet This not show up on it.
• Default becomes small text / icon link at end of posts. Big bird is still an option.
• Current tweet_this function becomes tweet_this_big. tweet_this does the same as tweet_this_small.
• Option to not show Tweet This on pages, only posts.
• Serialized options, instead of polluting the wp_options table with 5 records.
• Option to use www or http prefixing. www remains default for Th8.us; http is default for all other services.
• Option to put the title before the URL in the tweet text.
• Allow customization of “Tweet This” text from the options panel, with a text box. You can have it say “Post to Twitter” or whatever you want.
I’m planning to implement all this in v1.2 by the year’s end (sorry I keep putting this off).
Sites Using Tweet This [Updated 2008-11-17]
Banzai Beat
Blog 2100 [single post views only]
Blogs, Views, and Blues
Capitol Annex [single post views only, text link]
Collateral Learning
CSKnet: Build a Better Net
Daily Art Muse
Daily SEO blog
Dave and Thomas
DaytonaState.org
Ferticidio’s Geeks [modified v1.0]
greengrowthguys.com
I Like Shiny Things [modified v1.0]
Internet Business Logic Blog
John Taylor’s Blog
Lepton’s Blog
Me and Mine (yennygirl.com) [single post views only]
Monkeychapps.com [single post views only]
“Motivate Thyself”
Opinionated
Pajas Mentales Reloaded
= © PuBL|CGoDZ|LLa ® =
Referrals Unlimited Network [tweet_this_small() template tag]
Scary Personals
Steve Yastrow
The Hard Core DJ
the man in plastic’s - retrofocus ramblings
Thripp.com
Titoagur.to
TwiTip : Twitter Tips for Beginners
Up-And-Coming-Blogger
WordPress Web 2.0 Spot-Er [single post views only]
xybmobile
Add yours here; let me know about it in the comments.
Screenshots

1. Tweet This icon / link.

2. Roll-over effect: the icon lights up when you move the cursor over it. I use CSS sprites, so the two images are combined in one file, saving one HTTP request.

3. Arriving at Twitter, your reader can start typing about your blog post right away as the cursor is already flashing to the right of the trimmed URL.

4. The options page, under Settings > Tweet This, showing the defaults.

5. The drop-down URL Service box on the options page.

6. “Automatically insert a small link at the end of posts instead of a large icon at the top-right” is checked. ShareThis 2.3 (a different plugin) is also installed. As you can see, they go well together.
Frequently Asked Questions
= Why do the Tweet This URLs start with www. instead of http://? =
Because it’s shorter. Twitter is smart enough to convert www. to http://www. after you post a tweet, but www. is three characters shorter. When you only have 140 characters of space, three is a big deal.
= Why not use www prefixing on TinyURLs? =
That’s not what they provide. I could easily convert their API’s URLs to www., and it works fine now, but I can’t guarantee they will always support the www subdomain. Same for the other URL services. Because I’m the creator and owner of Th8.us, I can guarantee the www subdomain will always work with my service. You’d be surprised what a problem www vs. no-www support is.
= Why does Tweet This insert a + sign (space) to the end of the Twitter URL when “Include post title in tweet text” is disabled? =
When you click a Tweet This link, the post box will have the shortened URL followed by a space. Then, you can start typing about the blog entry immediately, instead of having to first hit the space bar yourself.
= Why is all your HTML and CSS squished together? =
To be insanely efficient! If eliminating non-required tabs and spaces saves 100 bytes per page load, and you get 10,000 per day, you’ve just saved 102 minutes of your visitors’ time per month, assuming they’re all on dialup and you’re not gzipping.
= What’s with all the get_ functions? =
Like in WordPress, the default Tweet This functions echo the result. Most can be prefixed with get_, which returns without echoing, so you can manipulate the data before displaying it.
= How does the cache work? =
Cached short URLs are saved to the postmeta table when a visitor views posts. For any future pageloads, those URLs are loaded, instead of pinging the Th8.us server (or is.gd, TinyURL, etc.). This works, because as long as the post’s permalink doesn’t change, the short URL from the third-party service doesn’t change.
The cache is invalidated by setting the existing short URLs in the postmeta table to “getnew” as needed. By reusing the old fields instead of replacing them, I don’t bump up the meta_id counter needlessly. When the next person visits that post, the get_tweet_this_short_url function in Tweet This sees this and gets a new short URL.
What triggers a cached URL as invalid? When you save a post (including editing and publishing), the cache is invalidated in case you changed the permalink. Secondly, when you change URL services under Settings > Tweet This or change permalink structures under Options > Permalinks, all the cached URLs are set to “getnew”. If you move your blog to a different directory or domain name, just change URL services and then change back to trigger a refresh on the cache.
When you deactivate the plugin, all the cached URLs are deleted.
For more details, look at these functions in tweet-this.php:
flush_tt_cache
delete_tt_cache
global_flush_tt_cache
global_delete_tt_cache
get_tweet_this_short_url
= Why doesn’t Tweet This show when I preview a draft post? =
Because I’d have to fetch a short URL if it did, and the permalink for the post isn’t set yet. It would be something like /?p=1, which would just waste an entry in TinyURL or another service’s database.
= Which short URL service do you recommend? =
Th8.us of course. I created it, and it’s going to be around for hundreds of years. is.gd is good and short, if you don’t mind your short URLs being case-sensitive. TinyURL has a slow API. I tested it, and it takes six seconds to load a page with ten Tweet This links on it if the TinyURLs aren’t cached.
= Can I just use the Tweet This functions without it adding icons to my blog? =
Sure! Activate the plugin, go to Settings > Tweet This, uncheck “Automatically insert Tweet This links,” and click “Save Options.”
= If I change URL services, will the old URLs continue to work? =
Yes. The short URLs are on third-party servers (Th8.us, TinyURL, is.gd, etc.), and they should never delete them.


Richard X. Thripp at 2008-11-17T20:05:31.You’re welcome! Looking good, although the Twitter favicon doesn’t go well against your dark background… I might have to do something about that.
[REPLY TO THIS]