Integrating the button into a WordPress template

If you don’t want to use the TweetMeme WordPress plugin which would add the button on your posts, you can easily integrate the button into your WordPress template. This is only available on self hosted WordPress installations and not WordPress.com.

In this example we will be adding the TweetMeme button into the bottom of the post page.

Open up your single.php template file located in http://yoursite/wp-content/themes/yourtheme/single.php you can do this on the online editor (if your files are writable), or on your local machine.

Navigate to the WordPress loop, it should be a piece of code like:

<?php if (have_posts()) { ?>
	<?php while (have_posts()) { the_post(); ?>
	<?php } ?>
<?php } ?>

In the while loop, before the closing brace you should insert the TweetMeme JavaScript code.

<?php if (have_posts()) { ?>
	<?php while (have_posts()) { the_post(); ?>
		<div class=”tweetmeme_button”>
			<script type="text/javascript">
				tweetmeme_url = '<?php the_permalink(); ?>';
			</script>
			<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
		</div>
	<?php } ?>
<?php } ?>

We are adding in the WordPress tag the_permalink() to make sure that we are passing the correct link to the TweetMeme button. This is not necessary but is available. If this is not included then it is possible for the same story to have different tweet counts.

1 Star2 Stars3 Stars4 Stars5 Stars (36 votes, average: 4.17 out of 5)