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.

Tweet this

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 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.

Therefore I put the following function in functions.php of the theme.

 * get a short/tiny url
 * @author: René Ade
 * @link:
if ( !function_exists('fb_gettinyurl') ) {
	function fb_gettinyurl( $url ) {
		$fp = fopen( '' . $url, 'r' );
		if ( $fp ) {
			$tinyurl = fgets( $fp );
			if( $tinyurl && !empty($tinyurl) )
				$url = $tinyurl;
			fclose( $fp );
		return $url;

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.

Tweet 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 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.

Tweet this





13 responses to “Tweet This with WordPress”

  1. DD32 Avatar

    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

  2. Frank Avatar

    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.

  3. […] только в чем ее прикол. Но все равно регнулся там ) И тут вот нашел небольшое расширение для кода вп, позволяющее […]

  4. Andrew Avatar

    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!

  5. Alex Avatar

    Hey Andrew,

    actually you just have to replace the function: get_permalink()

    Since it’s coming from WordPress. Hope that works for you.

  6. […] Üks väga hea nipp, kuidas oma WordPress’i blogile Twitter’i link lisada leidub siin: Tweet This with WordPress. […]

  7. Kurze URLs auf Knopfdruck…

    “Ich schick dir mal schnell einen Link” heißt es…und schon sind 6 Zeilen in einem Messenger gefüllt mit einem einzigen Link. Benjamin hat mir den Tipp gegeben das Tinyurl-Addon für Firefox zu benutzen, also habe ich es doch gleich …

  8. […] Все плюшки вп-инженеру. […]

  9. […] Все плюшки вп-инженеру. […]

  10. Mrtin Avatar

    I recently upgraded my wordpress install to 2.8
    The tweet this plugin causes problems on loading the front page but this is theme dependent.

  11. Prof. Drs. G.B.J. VanFrikschoten Avatar

    Tweet comment doesn’t function. It gives somehow the commentID first. and then the url + #comment

  12. danix Avatar

    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…

  13. Michael Shearer Avatar

    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).