Adding a Tweet button to a self-hosted WordPress blog

This content is 14 years old. I don't routinely update old blog posts as they are only intended to represent a view at a particular point in time. Please be warned that the information here may be out of date.

Yesterday, Twitter launched their official button for websites and keen-eyed observers will have noticed that now sports a shiny new “tweet” icon at the head of each post.  It’s pretty easy to do (Twitter generates the code for you – and has an FAQ for developers) but there may be a couple of things to watch out for, depending on your blog’s content management system.

I use self-hosted WordPress, and these were the changes I made to the site:

  • For my single posts, I just added the Twitter-generated code to an appropriate position in the single.php file.
  • For my main index page, I edited index.php, but didn’t want the generated link to be to the current page (my home page with several recent posts) – I wanted it to relate to the particular post that the viewer is tweeting from.  Adapting a tip from David Teng, I added two parameters to the link code (data-url="" and data-text="").

Now, by clicking the Tweet button on this post, the link generated relates to the post – and not the home page for the blog.

2 thoughts on “Adding a Tweet button to a self-hosted WordPress blog

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.