Tweet This with WordPress

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

<a href="http://twitter.com/home?status=Read:%20<?php the_permalink(); ?>">Tweet this</a>

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.

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

/**
 * get a short/tiny url
 * @author: René Ade
 * @link: http://www.rene-ade.de/inhalte/php-code-zum-erstellen-einer-tinyurl-ueber-tinyurl-com-api.html
 */
if ( !function_exists('fb_gettinyurl') ) {
	function fb_gettinyurl( $url ) {
	
		$fp = fopen( 'http://tinyurl.com/api-create.php?url=' . $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.

<a href="http://twitter.com/home?status=I%20recommend:%20<?php echo fb_gettinyurl( get_permalink() ); ?>">Tweet this</a>

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.

<a href="http://twitter.com/home?status=<?php echo urlencode( 'I recommend: ' ) . fb_gettinyurl( get_permalink() ); ?>">Tweet this</a>

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.

<a href="http://twitter.com/home?status=Comment%20Recommendation:%20<?php echo fb_gettinyurl( get_permalink() . '#comment-' . get_comment_ID() ); ?>">Tweet this</a>

Comments are closed.

13 comments

  1. DD32

    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

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

    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!

  4. Alex

    Hey Andrew,

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

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

  5. Mrtin

    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.

  6. Prof. Drs. G.B.J. VanFrikschoten

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

  7. danix

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

  8. Michael Shearer

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

    Thanks!

5 pingbacks

  1. Tweet this! для вордпресса. | Alex Volkov
  2. Kuidas Twitter’is edukas olla » Aarne bloog
  3. Chris im Netz - Software, Hardware, Tutorials und mehr..
  4. Добавить запись в twitter | Alex Volkov
  5. Добавить пост в твиттер | Alex Volkov