Update Twitter From your Blog If you are Logged In

Last updated: Aug 28, 2008

I don’t want to have to login to Twitter all the time to do updates. So what I made was a script that displays a small text box near my twitter updates on my blog when I am logged in. This is handy because not only does is display your tweets it also gives you an opportunity to update them while you are working on a blog post or just browsing around checking your own blog.

The Perl Script

I am using Perl to update my twitter status but you could just as easily use PHP and cURL. So this part could be modified if you would rather do it that way. In my opinion this is far easier though.

First download the Net::Twitter module from CPAN.

You can do this by executing the following line in your shell:

cpan Net::Twitter

Then make a file called twitterUpdate.pl somewhere outside your web directory and copy this code in adding your Twitter username and password.


use lib qw( /home/username/perlmods/lib/perl/5.8 /home/username/perlmods/lib/perl/5.8.4
            /home/username/perlmods/share/perl/5.8 /home/username/perlmods/share/perl/5.8.4 );

use Net::Twitter;
use JSON::Any;

my $twit = Net::Twitter->new(username=>'YOURUSERNAME', password=>'YOURPASSWORD' );

$result = $twit->update("$ARGV[0]");

Then add these lines to your blog’s template:

<?php global $user_ID; if ($user_ID) : ?>
<?php if (current_user_can('level_10')) : ?>
<form method="post" action="processTwitter.php">
<input type="text" name="tweet" style="float:left;margin-left:3px;" />
<input type="submit" />
<?php else : ?>
<?php endif; ?>
<?php endif; ?>

This will add a text box and submit button only if you logged in and have administrative rights.

Then all you have to do is use javascript or PHP to process the form!

Here is what the end result can look like…

Twitter Script

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Get notified on new posts or other things I'm working on