How to add Twitter Button Count on WordPress Blog

WordPress Apr 13, 2011 6 Comments

It is pretty simple and instructions available on twitter web site on how to create a twitter button count.

Step 1)

Login into your twitter account and then visit  Tweet Button page for further instruction on how to create the button.

In this example I’ll create twitter button count for Indian Recipes blog.

Twitter Button Count

Twitter Button Count

The fields are self explanatory. After you have selected the options, copy the code.

Step 2)

The code copied in above step is good for a single static page but not for WordPress blog. So we have to modify it to make it work for WordPress.

This is the original code

<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="freeindianrecip" data-related="southindianreci">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

We need to add following 2 lines for url and title for wordpress

data-url="<?php the_permalink(); ?>"
data-text="<?php the_title(); ?>"

And here is the modified code for wordpress:

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<a href="http://twitter.com/share" class="twitter-share-button"
data-count="horizontal"
data-via="freeindianrecip"
data-related="southindianreci"
data-url="<?php the_permalink(); ?>"
data-text="<?php the_title(); ?>"
>Tweet</a>

Step 3 – Add it in WordPress Single Post template

You can copy above code and paste it in single.php anywhere between while loop:

<?php while (have_posts()) : the_post(); ?>
..
<?php endwhile; ?>

Step 4 – Count stays at 0 fix

Well that is not all. After adding twitter button count, visit any post, click on button, post it to twitter and refresh the post using CTRL + F5. The count will always show 0. This is something which twitter should explicitly explain.

I had to spent a lot of time in finding the fix for twitter count stays at 0 fix. the solution is mentioned on http://dev.twitter.com/pages/tweet_button page – see the second last section.

The solution is to add data-counturl= variable which for wordpress will be

data-counturl="<?php the_permalink(); ?>"

So finally the twitter count button code will be:

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<a href="http://twitter.com/share" class="twitter-share-button"
data-count="horizontal"
data-via="freeindianrecip"
data-related="southindianreci"
data-url="<?php the_permalink(); ?>"
data-counturl="<?php the_permalink(); ?>"
data-text="<?php the_title(); ?>"
>Tweet</a>

LOL! It still doesn’t work, the count stays at 0. When you submit it increases the count but when you refresh the page the count goes back to 0.

I just checked Twitter Button FAQ page

My count doesn’t seem to be going up, is something wrong?

To improve performance we cache the count before displaying it. The cache is updated frequently but on some occasions it may look like your count is not increasing while the cache is updated. In addition, the count only includes public Tweets meaning a Tweet from a protected account will not cause an increase.

It doesn’t help either. The caching doesn’t explain why count always stays at 0. Even after days the count still remains 0.

On stack overflow post Twitter Button Doesn’t Count there are some solutions provided but none of them seem to work for me.

I added following line

data-via="#"

So the final code is:

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<a href="http://twitter.com/share" class="twitter-share-button"
data-count="horizontal"
data-via="freeindianrecip"
data-related="southindianreci"
data-url="<?php the_permalink(); ?>"
data-counturl="<?php the_permalink(); ?>"
data-text="<?php the_title(); ?>"
data-via="#"
>Tweet</a>

You can move following line in footer just above closing body tag

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

Above code seems to work for new posts but for old posts it still doesn’t increase the twitter button count.

I’ve been observing the twitter count button and it seems it works. You might not see the counts immediately but after few days/weeks it ultimately shows up. See the live example on this post itself. You will see counts there. Initially it showed 0 but as people started tweeting the count showed up.

Comments

6 Responses to “ How to add Twitter Button Count on WordPress Blog ”
  1. Tim Ware

    Did you find a fix for the Twitter count not incrementing?

    I’m using:

    <a href="http://twitter.com/share" data-text=""
    data-url="" data-counturl="
    class="twitter-share-button">Tweet

  2. Tim Ware

    Let’s try that code again:
    <script src=”http://platform.twitter.com/widgets.js” type=”text/javascript”></script>
    <a href=”http://twitter.com/share” data-text=”<?php the_title(); ?>”
    data-url=”<?php the_permalink() ?>” data-counturl=”<?php the_permalink(); ?>
    class=”twitter-share-button”>Tweet</a>

  3. rohit

    Hey dude is it really works and updates the count?

  4. rohit

    hey tim ware plz let me know is it works or not

  5. Anil Singh

    Hello,
    dear this code is not incrementing counter, stays 0.

    <a href=”http://twitter.com/share” data-text=””
    data-url=”” data-counturl=”
    class=”twitter-share-button”>Tweet

    please guide me——–

    • Ajay

      Anil,
      That code works. I’m using this same code on all of my blogs and it works. See the Twitter count on this post itself at the top – it shows 12 which means it works.

      I think there is a twitter count cache which doesn’t show count immediately but over the course of time (days/weeks) it will show up.

Leave a Reply to Tim Ware