Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Add Twitter’s Official Tweet Button in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Add Twitter’s Official Tweet Button in WordPress

Earlier today, Twitter announced their official Tweet Button which does the exact same thing as the Tweetmeme buttons except it is made by twitter. A lot of people are already switching (this include top corporates and small bloggers that follow @wpbeginner on twitter) because this button offers additional customization plus it has the option to recommend account following. In this article, we will share how you can add twitter’s tweet button in WordPress.

Official Tweet Button

This code can be installed in single.php, loop.php, index.php, page.php, category.php, and archive.php as long as it is placed within the post loop.

<script src="" type="text/javascript"></script>
   <a href="" class="twitter-share-button"
      data-url="<?php the_permalink(); ?>"
      data-text="<?php the_title(); ?>"
      data-related="syedbalkhi:Founder of WPBeginner"


Try tweeting using the button above.

If you notice in the code above, we are using the data-attribute of anchor tag to tell twitter exactly what we want it to do. Below is the explanation of what each property means:

  • data-url – This fetches the URL that you want to share (You do not need to change this).
  • data-via – This tells twitter who was the original tweeter by adding via @wpbeginner (Make sure you change it to your twitter account).
  • data-text – This fetches the title of your post (You do not need to change this).
  • data-related – This adds recommended users to follow. You are allowed up to two Twitter accounts for users to follow after they share content from your website. These accounts could include your own, or that of a contributor or a partner. The first account is the one that is shared in data-via property. (Make sure you change it to one of your other twitter accounts, or remove it). If you don’t, then you will be recommending @syedbalkhi (Founder of WPBeginner). The correct format to enter data in this variable is twitterusername:Description of the User
  • data-count – This tells twitter’s script which style of button you want to show. You have three option (vertical, horizontal, none).
  • data-lang – This variable tells twitter which language it should be in. Default value is ‘en’ for English, and we have left it at that.

We recommend users to use the data-attribute of anchor tag method because it keeps the code clean and short. Even though query string parameters are a convenient way to share your page, they can make your anchor tag very long, and a long URL is difficult to maintain especially when you have to URL encode parameters. Below is the query method for those who choose to use it:

<script src="" type="text/javascript"></script>
<a href="<?php echo urlencode(get_permalink($post->ID)); ?>&via=wpbeginner&count=horizontal" class="twitter-share-button">Tweet</a>

If you just want to add a tweet button on a static page, then you can use Twitter’s Tweet Button Generator.

Thanks to Otto for notifying us in the comment below that his plugin, Simple Twitter Connect, fully supports the official tweet button.

Additional Resources:

Supplemental Documentation for Tweet Button

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. jafar says:

    thank man :)

  2. RandyDueck says:

    Thanks for the quick response. I’m not a programmer, do you know where I can get a peice of code and paste it in? Re: Count, I’m thinking then that when testing, If I use the tweet button to post to post to my own page, then twitter doesn’t tink it’s legit? However, I have another twitter acc’t that I tried tweeting to and it didn’t increase. Is there a way I can test it to see that it’s working? How do I know it’s a legit tweet or not? I’m surpirsed that Twitter seems difficult compared to FB @wpbeginner

  3. wpbeginner says:

    @RandyDueck Twitter uses the new shortner which only shorten links for their algorithm to make sure it is not SPAM… then it pastes the unshortened version… You have to utilize another API and write a bit of custom code to get the custom short URLs to work there…As far as the count, twitter button only shows count that it thinks are legit.

  4. RandyDueck says:

    Thanks for this great little code snippet. I want the twitter button to be in the post excerpt and below the title of the single-post page. the code works fine. gives the correct title etc. however, it does not shorten the link, and after tweeting, the tweet count doesn’t go up. the site is Thank your response

  5. KatrinaMoody says:

    Gretchen – all links should trace back to your permalink – so your permalink should be the source URL, not your short link … make sense? YOu can’t yet change the URL shortener if you use the official Tweet button ( I looked into it because I wanted to do a shortlink).

    Adding this so long after the original was posted because others might have that question!

  6. KatrinaMoody says:

    I thought I would note that some themes change the value for the title and post url – meaning that the code will tweet with your code in quotes. I got a couple questions on Twitter over that one. I just found the different variables that worked for my site and replaced them. So …

    Line three: data-url=””

    was changed to %permalink% – which was my theme’s replacement

    Line five: data-text=””

    was changed to %post-title% – again my theme’s replacement

    I wouldn’t have been able to do this without your tutorial! Thanks SOOOO much! You are now my go-to resource!

  7. idowebmarketing says:

    @wpbeginner @idowebmarketing Okay, there are two locations of this code, and I had only updated one!

  8. wpbeginner says:

    @idowebmarketing @wpbeginner data-via is the only thing that needs to be modified in order to make the change.

  9. idowebmarketing says:

    @wpbeginner @idowebmarketing Thanks – I added new blog posts to two of my blogs yesterday after implementing the updated code, however it still recommended wpbeginner on both. Is there any section of code that also needs to be updated?

  10. wpbeginner says:

    @idowebmarketing On old posts it takes twitter while to figure it out. On new posts it should be fine.

  11. idowebmarketing says:

    My blog is still recommending wpbeginner and I have updated the code. Here is the edited code:

    <script src=”” type=”text/javascript”></script> <a href=”” data-url=”<?php the_permalink(); ?>” data-via=”idowebmarketing” data-text=”<?php the_title(); ?>” data-related=”imnewsdaily:Daily updates from the internet marketing industry” data-count=”vertical”>Tweet</a>Any idea what I am doing wrong?

  12. Richard says:

    Thank you for posting this!! I have been trying to slove this problem for about three days now…

    do you have the same code for the facebook like button?

  13. Gretchen says:

    I use the WP-generated shortlinks when tweeting about my posts, and have had troubles with those always showing in my Tweet count. The final bit of code on this site seems to work, though if you click to view the search results in Twitter it only shows the links to the actual permalink, not the links to the shortlink as well: It’s too bad WordPress and Twitter don’t count those as the same link somehow…

  14. Zakir says:

    I can add javascript in header but it will degrade YSlow rating, currently it is B rating. I really work hard to make B from D. I opened a ticket to twitter technical team, they just sent me email providing some links which may help me to get my answer or open a new ticket if not. Thats so stupid. Twitter people think we didn’t see other answers! thats so frustating. Linkedin doing quite well , their share button updates within a minute.

  15. Deepanshu says:

    hello sir :-) sir how do i use this for pages ? i was able to do it for posts

  16. Zakir says:

    In my above mentioned website, i used your code, index.php, single.php and other php file within loop.
    I can click the twitter tweet button and it shows count number, but if i refresh the page, that value goes away. Now after may be 1 hour if i check that article page, i can see the new count value. e.g. before i clicked the button it was 0, after almost 1 hour it shows 1. My question is why it took 1 hour to update. it should be updated immediately isn’t it?

    Now in front page which index.php, i used the code and again i used in single.php. User clicks an article from index.php and comes article page which is in single.php. The problem: count value is not synchronising the count from the index.php to single.php. I check it later it is but after almost 1 hour. so it is synchronising after almost 1 hour.

    Why? any idea.

    Recently I changed the .htaccess file to add expire headers to get some speed. i am getting that but today i removed that line and back to default .htaccess file. Do you think its something with .htaccess where caching is controlled by hour, minutes, seconds, months or even year. Even though if I enable caching, the twitter java script which is twitter server can’t force to use caching.

    Now I am not sure where I should look for to get immediate count value. client wants to see immediate count and synchronise with article page.

    Please check the to get an idea.

    Any clue or idea will be a great favour.

    I used following code for tweet count button:

    <a href="; class="twitter-share-button"


    • Editorial Staff says:

      Zakir, We think this is a common bug going on with twitter API. We have been noticing a similar thing with our tweetmeme button regarding the count. It synchornizes the count almost after 20 – 30 minutes. Perhaps try adding the script part in your head codes and then display the a href part where you want the button to display might help.

  17. Simon says:

    I don’t get it. Using the data url link/button above gives just a link and nothing else. Has something changed there or why doesn’t it output the values?

    • Editorial Staff says:

      Note, to get the_title(); or the_permalink(); you have to place this code inside the loop. If you place it outside a loop, then it will not give you a desired value.

  18. coz says:

    ARGH. This is what I tried but im replacing the greater-than/less-than symbols with brackets so it will hopefully show:

    [?php the_title(); ?] #[?php the_category(‘,’); ?]

  19. coz says:

    um… last comment isn’t displaying the php… sorry. This is what i tried: (hopefully this will work)

  20. coz says:

    For DataText, im looking to add the title and category of the post as a hashtag. I can’t seem to figure out how to do it, being that I’m not too familiar with php.

    I tried: >>> data-text=” #”

    but that didn’t work. Any idea of how to do this?

  21. S.Pradeep Kumar says:

    Nice tutorial. Finally I implemented that button on my blog. It will be nice if you can make a tutorial on how to customize it. I hate the default color. : |

  22. Calítoe.:. (Cristina MJ) says:

    I’ve noticed that the count displays horizontally even if one specifies data-count=”vertical”. Does anyone know what’s wrong with that?

    • NG says:

      I’m having the exact same problem. Any idea how to fix it?

      • Editorial Staff says:

        Refresh the page… This usually happens when the twitter script is not loading fine. Try placing the twitter script part in the head section.

  23. CharityHisle says:

    I think I’ll stick to the tweetmeme version until the plugin is completely developed with all of the options for placement.

  24. Rarst says:

    There isn’t much sense in filling out every setting, most of them work just fine without being set and will pick things like current URL and page title by default.

    Non-standard tag attributes will not validate (at least not as XHTML Transitional in my experience so far). Query arguments may not look as tidy, but they are more established and reliable.

  25. Konstantin says:

    Alternatively, you could use WordPress’ very own add_query_arg() function, to create the url.
    Nice post though, and quick, too. ;)

    • Editorial Staff says:

      Yup, we had to put it out quick because a lot of our twitter users said they wanted it.

  26. Amie says:

    Is there any chance I don’t have “The Loop” because I keep looking for this (or even a part of it) in my index file and it’s not there. Gah, I was hoping this would be easy! :-(

    • Editorial Staff says:

      That really depends on what theme you are using. Every theme must have the loop to show the recent posts. You can try Otto’s plugin.

  27. DJ NightLife says:

    I used to have Topsy buttons… was about to switch for the official button but… It seems this version have a problem recognizing the real count of tweets. I had a post of 52 tweets and now it shows 8.

  28. Ted Thompson says:

    Helpful article, many thanks!

  29. josemv says:

    Excellent, thanx !
    Any chance to include custom url shorteners, such as or ?

    • Editorial Staff says:

      No, twitter is using their very own service to verify the quality of link that is being tweeted.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.