WordPress: The Best Tweet This Link Ever

Reading time: About 0 minutes

With the right people sharing your blog entries on Twitter, you’ll receive a good amount of quality referrals. One way this occurs is a retweet from frequently retweeted people as seen here.

The other way is to make it super simple for readers to tweet your blog entry. That’s where this post helps. If you have a self-hosted WordPress blog, add this code to your /wp-content/themes/[current theme]/single.php file where you want your own “Tweet This” link. [TinyURL shortening code originally from here]

1
2
3
4
5
<?php
$shortenedurl = file_get_contents('http://tinyurl.com/api-create.php?url=' . urlencode(get_permalink()));
?>
 
<a href="http://www.twitter.com/home?status=<?php echo str_replace(' ', '+', the_title_attribute('echo=0')); echo '+' . $shortenedurl; ?>" title="Share <?php the_title_attribute(); ?> on Twitter">Tweet This</a>

8 comments skip to comment form

  1. Drew Douglass said— 6 minutes later

    Nice work Brian, I am working on a WordPress plugin very similar to this, I was actually inspired by your ‘Create TinyURLS’ with 1 line of php’ article. Your always showing great and simple ways to use php to have some fun and build some great things. Thanks for sharing.

    #1
  2. Robert Banh said— 17 minutes later

    One line code is great. And short blog postings like this is great too! Keep it up man.

    #2
  3. metroscapeGuy said— 3 hours later

    how could you go about encoding the hash (#) in the tweeturl string so that the actual tweet could be tagged automatically as well?

    #3
  4. Mimojito (aka Efren) said— 1 week later

    Brian, great post as always. Seems easy enough.

    @Mimojito (aka Efren)

    #4
  5. Nixy Valentine said— 2 weeks later

    After trying some of the WordPress plugins out there, I found this was much easier and gave me a lot more control.

    Thank you for posting this!

    #5
  6. Lewis said— 2 weeks later

    This now does not work in firefox (not tested on others)… before “/home” you need to add “/timeline”

    for example:

    http://twitter.com/timeline/home?status=

    Best wishes

    Lewis Clayton

    #6
  7. Lewis said— 2 weeks later

    By the way… GOOD JOB! =0>

    Sorry… was in a rush… I think that with Twitter updating things is why this needs to be put in… I have no idea! :-)

    Lewis Clayton

    #7
  8. Brian Cray said— 2 weeks later

    @Lewis Clayton

    I would test that again… time timeline/home page is a 404.

    I tested my “tweet this” links which use the same code and it works fine.

    #8
  9. Respond to this post—

Return to navigation
274