How to quickly add the Tweet Button to your TwentyTen Template in WordPress

Just added the new Tweet Button to this WordPress blog that uses the standard TwentyTen template. It’s really quick and simple…

Firstly, I added this to a child of TwentyTen, which makes it much easier to configure the template and prevents me losing functionality when it updates. But in any case it is really just a matter of adding this to your functions.php file in your Appearance->Editor.

Just paste in this code and save it…


function add_pre_content($content) {
	if(!is_feed() && !is_home()) {
		$content = '<a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-url="'.get_permalink().'" data-via="shawndm">Tweet</a>
			<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>'.$content;
	}
	return $content;
}
add_filter('the_content', 'add_pre_content');

Which will require just one change, change my “shawndm” to your Twitter account name and that’s it – it will appear just before the content and just after your title.

Be Sociable, Share!
  • Thank you for information shawn. Does the special plugins are not avaliable yet?

    Btw, will try to figure out hot to use this code 😉

    • Shawn Du’Mmett

      I tend to add things into the code because I don’t like adding unnecessary functionality if I don’t want it, I like to understand everything that is happening on the site and I guess because I can 🙂 The advantage to me is that I know how everything is working and can tie in custom functionality later when the opportunity comes.

      So please don’t worry if you cannot add the script – a quick look at popular ‘twitter button’ plugins on WordPress comes up with two that I see will do the job:
      http://wordpress.org/extend/plugins/twitter-button/
      http://wordpress.org/extend/plugins/new-twitter-button/

      If you try them then it will be great to see which one you go for. That’ll be useful for anyone else reading this later.

  • Thank you for information shawn. Does the special plugins are not avaliable yet?

    Btw, will try to figure out hot to use this code 😉

    • Shawn Du’Mmett

      I tend to add things into the code because I don’t like adding unnecessary functionality if I don’t want it, I like to understand everything that is happening on the site and I guess because I can 🙂 The advantage to me is that I know how everything is working and can tie in custom functionality later when the opportunity comes.

      So please don’t worry if you cannot add the script – a quick look at popular ‘twitter button’ plugins on WordPress comes up with two that I see will do the job:
      http://wordpress.org/extend/plugins/twitter-button/
      http://wordpress.org/extend/plugins/new-twitter-button/

      If you try them then it will be great to see which one you go for. That’ll be useful for anyone else reading this later.