ExpressionEngine plugin - Send Tweet

18 Apr

This week, I wanted to find an easy way to maintain a Twitter channel for Flashmagazine. By following this Twitter user, you can stay up to date on current Community News, Reviews and Tutorials and get notified the instant something interesting happens.

I just love the ease of ExpressionEngine. Since I only could find EE plugins that can read from Twitter, I figured I’d make a plugin that can send messages as well. After some thinking, I made/setup/tested/implemented one in just 4 hours. I’m by no means a whiz with PHP, but making EE plugins is really easy and well documented.

I’ve tested the plugin for a couple days and it appears to work well, so I guess it’s time to release it for others to use.

About the plugin

This plugin will send messages to your Twitter.com account. I originally made this to set up an automated Twitter feed for my site Flashmagazine. Just seconds after I publish a new story, my subscribers can be notified of it. You could use this for a lot of other purposes as well, so feel free to email me with other use cases smile

The plugin receives a string of text and send it to Twitter. To prevent this from happening the script will write the text string to a file. Before sending another Tweet, the script will check against the contents of the text file on the server. This is a little “expensive” but should only be a performance hit on huge sites.

Setup

You will need to do some manual editing of this PHP file for this to work:

- Open the plugin PHP file in your favorite PHP editor
- Change the username and password variables at the top of the class to match your Twitter.com credentials
- Copy the file to your ExpressionEngine plugin folder
- Open the plugin settings in the EE Control Panel to look up syntax and check that the script installed properly

That’s all. The plugin works as a tag pair:

{exp:send_tweet}Text to send to twitter{/exp:send_tweet}

Usage Send_Tweet plugin for EE

To use the plugin to notify Twitter of new stories, paste this into your RSS template:

{exp:send_tweet}{exp:weblog:entries weblog="news" limit="1" rdf="off" dynamic_start="on" disable="member_data|trackbacks"}{title}: {comment_url_title_auto_path}{/exp:weblog:entries}{/exp:send_tweet}

This code will send the title and URL from the latest story published to Twitter. If the URL is long, Twitter will use tinyurl.com to shorten it for you. Keep in mind that Twitter won’t accept messages over 140 characters so keep the titles short to make sure the URL’s are not cut off.

Make sure you set the weblog parameter to reflect how you set up your site and that {comment_url_title_auto_path} works with your setup (You may need to use a different tag such as title_url, just check what is used in your RSS file). You can put this code anywhere in the RSS file. I have mine at the top of the PHP file.

How to make Twitter update automatically

Feedburner is a service that will grab and republish your RSS file. They offer a service that you can “Ping” when you have updated your site. They will then go grab the latest version of your RSS feed and re-publish it.

ExpressionEngine can “ping” FeedBurner automatically. All you need to do is to set up an account with FeedBurner and open the EE Control Panel. Go to Admin -> Weblog Administration -> Default Ping Servers and add the URL http://ping.feedburner.com.

(Note: you may use any other XML-RPC service. Feedburner is just a suggestion from me. http://rpc.pingomatic.com is another good service that’ll do the same, but for a bunch of sites at once)

Debugging

To see debug output from the plugin, edit the plugin file (as noted in the comments) and wrap the output with an empty node like this:

{exp:send_tweet}{exp:weblog:entries weblog=“news” limit=“1” rdf=“off” dynamic_start=“on” disable=“member_data|trackbacks”}{title}: {comment_url_title_auto_path}{/exp:weblog:entries}{/exp:send_tweet}

If you don’t add that extra node, Firefox (and maybe other browsers as well) will request the document twice, causing incorrect debug messages to be shown in some cases.

Installing plugins in EE

Download and unzip this file and copy “pi.send_tweet.php” to your plugins folder (/sysfolder/plugins/). That’s it smile

18 April 2008 at 6:10 pm

7 Responses to ExpressionEngine plugin - Send Tweet

  1. avatar photo
    jr 30 April 2008 at 6:03 am #

    Any idea why my tweets always end with the first / in the entry?  I’ve hard coded part of the URL instead of using the {comment_url_title_auto_path} tag, so my links don’t show up in twitter.  Otherwise this is great, thanks!

  2. avatar photo
    Jensa 23 May 2008 at 5:03 am #

    Hi jr,
    No idea really, but take a look at the “lasttweet.txt” file on your server and look at what you are sending to Twitter? Should give you a pointer…

    I have an issue with the plugin right now where it has stopped working. Will repost here when I find out what’s happened…

    J

  3. avatar photo
    Jeramiah 01 August 2008 at 2:43 am #

    And literally nothing happens to twitter.  I have tried a lot of variations but the directions are so simple and yet it’s just not working.  Any ideas on what might be wrong here or some considerations?  Thanks

  4. avatar photo
    jeramiah 01 August 2008 at 3:39 am #

    nevermind, got it working.  You are awesome btw… have you seen the “Twitter This” link on Get Satisfaction.com?  Now that would be hot to have as a plugin.

  5. avatar photo
    srdha 23 May 2009 at 3:21 pm #

    i gust want to say some thing “great job”

  6. avatar photo
    Rob Q. 02 September 2010 at 11:06 pm #

    I’ve been using this plugin for about 7 months flawlessly but today with Twitter’s move to Oauth for api it stopped working. Any chance you might update it to support that method?

    Rob

  7. avatar photo
    Jensa 06 September 2010 at 3:52 am #

    Hi Rob,
    Had a look at this tonight and I think this is the end of the line for this plugin. I use it myself, but it looks like the OAuth process will be a little too much fuzz for little? I’ll be looking for alternative solutions myself I think, but feel free to update and change it if you’re up for it?


    J

  8.