Been working on a new project that will go into previews for selected friends and family later this week and go into general release a few days later, once I've had a chance to fix any issues my preview users find.

One part of the project was to add "Tweet This" links to certain items on the site, where clicking on the link would send the user to Twitter and fill in the suggested text of the tweet for them.

It's actually a lot simpler than you think. The format is: http://twitter.com/?status=[URL encoded tweet text].

Now many of you are asking how you "URL encode" the tweet text. Well, if you're using PHP, you use urlencode('text'); where "text" is the text of your tweet. If you want to do it in JavaScript, the PHP.js library has a javascript equivalent for PHP's urlencode.

But here's a little trick I didn't know about until I made this mistake. Make sure your link goes to twitter.com, not www.twitter.com. If it goes to www.twitter.com, the text doesn't get properly decoded. So, trying to tweet Creating a "Tweet This" Link - http://www.brainhandles.com would look like Creating a %22Tweet This%22 link - http%3A%2F%2Fwww.brainhandles.com, and nobody wants that.

Share
8 Responses to “Creating a "Tweet This" Link”
  1. Brian says:

    Great find on omitting www from the Twitter link. You saved me a few more hours of frustration!

  2. al says:

    What happens if the person who clicks the link is not logged into twitter before clicking?

  3. Nic says:

    It seems like not all characters need to be encoded. I think I got away with leaving spaces, slashes & colons. Does this sound like it would present an issue?

  4. Patis says:

    I'll second that thank-you for the tip about the link.
    Only thing that would be nice is if you mentioned how to integrate bit.ly (or something similar) into the link.

    Thanks for an awesome straight-to-the-point tutorial!

  5. Buckaroos says:

    Found with this code that if the user is not logged into twitter, after user logs in the message in the anchor href tag is lost (i.e., the "What's Happening?" text box comes up blank) whereas if the user is logged in, the message in the tag appears in the "What's Happening?" text box.

    Any work around for this issue?

    Best regards, Buckaroos

  6. @Buckaroos and others - use this form:

    http://twitter.com/home?status=URL encoded tweet text]

    Note the addition of /home to the URL. This passes the tweet through the login redirection to your status as you would expect.

    cheers!

  7. Stacy says:

    THANK YOU! This HTML link was exactly what I was looking for. I really appreciate it. Now, I just need to know what it is for Facebook! :) Cheers.

  8.  
Get an angel for your site An Angel Watches Over This Site