Add a Tweet Button to WordPress Posts

Want to add a Twitter Tweet button to your WordPress posts? It’s super easy:

Horizontal Button:


[php]
<a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php echo get_permalink(); ?>" data-count="horizontal" data-text="Check out <?php the_title(); ?> on < ?php bloginfo( ‘name’ ); ?>" data-via="YOURUSERNAME">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
[/php]

Vertical Button:

[php]
<a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php echo get_permalink(); ?>" data-count="vertical" data-text="Check out <?php the_title(); ?> on < ?php bloginfo( ‘name’ ); ?>" data-via="YOURUSERNAME">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
[/php]

Simple, No-Count Button:

[php]
<a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php echo get_permalink(); ?>" data-count="none" data-text="Check out <?php the_title(); ?> on < ?php bloginfo( ‘name’ ); ?>" data-via="YOURUSERNAME">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
[/php]

Customizing Output:

  • Change out data-via="YOURUSERNAME" to your your own Twitter handle.
  • The inclusion of data-text="Check out < ?php the_title(); ?> on < ?php bloginfo( 'name' ); ?>" isn’t necessary, but it’ll give you greater control over the intial text.
  • I’ve added these to my sidebar.php (and are only displayed on pages using my single.php template) but this can be customized to be included on pages, archive pages, etc.

Updates:

  • rawurlencode(get_permalink()); isn’t necessary and can actually cause the widget to report incorrectly if placed outside of the loop. Fixed code examples.

3 Replies to “Add a Tweet Button to WordPress Posts”

Comments are closed.