Web "/>

ExpressionEngine plugin - Send Tweet

18 April 2008 at 6:10 pm

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 😊

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 😊