user-pic

How to add a "post to Twitter" button to every entry?

Vote 0 Votes

I would like each post to have a "post to twitter" link/button on it.

Additional, on the home page, where I have snippits of recent posts, it would be nice to add a post to Twitter button/link for each snippet, just below the "Continue reading ..." link.

I've found instructions for how to do this on other Blogging platforms, but not for Movable Type.

Reported on Movable Type 4.2

13 Replies

| Add a Reply
  • Not sure if this is what you're looking for but this is how I did it on my website:

    <a href="http://twitter.com/home?status=RT via %40yourtwitter%3A <$MTEntryTitle encode_html="1"> <$mt:EntryPermalink encode_url="1"$>" target="_blank">Tweet This</a>
    
  • Thank you. I'll try it out.

  • One potential problem is that twitter will not auto shorten long url's.

    If you have a bit.ly account (free) you can use their API like this

    <script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=BITLY_ID&apiKey=BITLY_API_KEY"></script>
    <script type="text/javascript">
    BitlyCB.shortenResponse = function(data) {
    var s = '';
    var first_result;
    // Results are keyed by longUrl, so we need to grab the first one.
    for (var r in data.results) {
    first_result = data.results[r]; break;
    }

    // Now get the shortURL
    shortURL = first_result["shortUrl"];
    document.location = "http://www.twitter.com/home/?status=Reading:+" +
    escape(document.title) + "+" + shortURL
    + "+{via+@me}";
    }
    </script>
    <img src="./amazing_twitter_graphic.png" onClick="BitlyClient.shorten(document.location, 'BitlyCB.shortenResponse');">

    Note: Not my code, taken from a post on the ProBlogger 31 Days to Build a Better Blog forums.

  • I have actually been looking for something like this myself. However using either escape(document.title) or includes %20 for every space when the page redirects to Twitter.

    How do you get rid of the %20 for spaces? Anyone know?

    Ken

  • Try replacing

    escape(document.title) + "+" + shortURL

    with

    document.title.replace(/ /g,"+") + "+" + shortURL
    • The only problem is that I need this:

      <$MTEntryTitle encode_html="0">
      

      Not document.title. But this is a step in the right direction. Also I tried your above code Rob and no go.

  • It puts in the + signs in as needed to alleviate the %20, but it just fails to load the page.

  • Twitter is under DDOS attack at the moment making it difficult to test :)

  • Thanks all for the tips. I ended up using the TweetMeme.com script. Works well, includes a count of how many times the URL to a post has been tweeted.

  • Hey Rob, many thanks! I got it working, and yes the 2 days of wonkyness because of the DoS made testing no fun. Check out my code here if you wish:

    http://bgviewsnetwork.com/dev/2009/08/setting-up-rewriterules-for-profile-and-tags/

    Ken

  • Here is the script provided by Rob but it now opens in a new window. The only problem is that window.open will not work if Safari is set to block popups. Oddly even if Firefox is set to block the popups, window.open still works.

    <script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=BITLYID&apiKey=BITLYAPI"></script>
    <script type="text/javascript">
    BitlyCB.shortenResponse = function(data) {
    var s = '';
    var first_result;
    // Results are keyed by longUrl, so we need to grab the first one.
    for (var r in data.results) {
    first_result = data.results[r]; break;
    }
    
    

    // Now get the shortURL
    shortURL = first_result["shortUrl"];
    windowOpenUrl = "http://www.twitter.com/home/?status=" + document.title.replace(/ /g,"+") + "+" + shortURL + "+(via+@example)";
    window.open(windowOpenUrl, 'new_window', 'width=600,height=400');}
    </script>
    <img style="cursor: pointer;" src="/images/tweet_this.gif" title="Tweet about <$MTEntryTitle encode_html="0">" onClick="BitlyClient.shorten(document.location, 'BitlyCB.shortenResponse');">

  • Hey Rob, many thanks! I got it working, and yes the 2 days of wonkyness because of the DoS made testing no fun. Check out my code here if you wish:
    http://www.solicitors-in.co.uk/

Add a Reply

If you need to share template code, replace all the "<" signs with "&lt;" or use this utility.

Forum Groups

1773 6162

Last Topic: Excluding categories from blog by kholechek on Feb 9, 2012

86 302

Last Topic: website entries by masoud on Oct 26, 2011

1429 5077

Last Topic: What apocalypse hit this community in the middle of 2011? by 75th on Feb 10, 2012

694 2908

Last Topic: Blogspot to Movable Type by sdude on Feb 8, 2012

84 291

Last Topic: How to have some other characters in entry basename automatically written by Afshin Haghighatnia on Dec 22, 2011

173 737

Last Topic: About the MT version stated in HTML source by Alex E. Schneider on Feb 7, 2012

190 565

Last Topic: Analytics Reporting by michael webster on Feb 5, 2012

48 210

Last Topic: An idea and also a request by Afshin Haghighatnia on Jun 29, 2011

64 246

Last Topic: jQuery in MT 5.1 still at 1.4 - why? by perlmonkey on May 25, 2011

code.sixapart.com

137 478

Last Topic: Getting a thumbnail with xpath by Peter on Mar 13, 2011

222 720

Last Topic: Custom Field for Asset Not Appearing by android on Feb 9, 2012