Tweet This with WordPress

Twitter seems to continue to gain in popularity and one or the other has even completely move - not a blog anymore, only an address in Twitter.

After going through my subscriptions in my feed reader, I noticed several articles where one or the other Plugin was recommended, which place a Twitter button to recommend an article on your Twitter. Most Plugins have a function that takes place in the template of the integrated theme.

As so often, this is a little exaggerated, and as in the past with the inclusion of links to social bookmark services, there is also a simple solution with the help of a function in the WordPress core.

Twitter provides a URL that exactly accomplish the purpose to recommend posts or links. Using the Permalink-Template Tag of WordPress it is easy and quick to generate the link.

You just have to integrate the syntax in the template of the theme, usually in single.php. In some cases in the index.php as well, depending on the structure of the theme.

Expansion via Tiny URL

A small extension I would like to add, because one or the other user wants to shorten the URL, because 140 characters is not much. But there is a simple solution, which requires a function to create from our Permalink an abbreviated URL.

There is not much coding necessary, because the service of TinyURL.com provides an API, which takes over this task easily. I use the necessary function of René, since he already has published it on his blog.

The integration of the link in the template of the theme must be, however, a little different, because the_permalink() is created for the output and gives not only the value of the URL. But here is the clean separation into WordPress by echo and return functions extremely useful, and so you can easily and quickly use the basis of this template tag - get_permalink().

Now there is the ability to create a shortened URL and we can use the link as described in the beginning. A possible syntax could look like this.

Encode URL

Because there were already some questions about it, I will give a short hint. The URL can not simply hand over a blank space, it is similar with other characters and you have to encode these characters. This can be done via PHP, or use a small table for references. This explains, why I use %20 in my text - %20 stands for a space.

If you work with the help of the PHP function, then the above syntax could look like this.

Tweet comments

Also recommend the individual comments should thus work, because that is basically just a link. Since the URL is much longer, I show the syntax of the above function to generate a short URL. Here in this blog it is not active and was not tested - so if improvements be known or obvious mistakes, then I ask you to let us know in the comments.

13 comments

Just remember, If you dont run with caching enabled (supercache or wp-cache), This could end up slowing your pageloads significantly down.

You'd be far better off to cache the tinyurl link in a custom field, which would complicate things, But would reduce the number of external connections required from one-connection-per-page-load to one-connection-per-blog-post

Yes, this is correct. I think, its not so nice when you use custom fields for this little assigment. Caching pluigns give it static pages and then dont work this dynmaic function.
Thanks for your comment, this is realy important for users, who use a plugin for caching.

I searched forever to find this and am very impressed. Already got it working great in two blogs. There is a "tweet this" plugin available, but the code here is much easier to work with (styling, etc.)Any ideas on how to integrate this into phpbb3? This blog seems to focus on WP, but hey, you can't blame me for asking!

Hello, I have a small form on the homepage that allows my user to post some asides without going to the post section of the admin area using wp_insert_post(), what should I do to make this form submit the aside to twitter too?

btw great site, it's my everyday inspiration for ideas on how to improve my WP blog...

Awesome solution, Frank! Just what I was looking for. Can you do this with Facebook e.g. I want someone to click "Like' (and not use the button, just the word "like") and have it share the permalink to facebook (in shortened fashion).