Auto-publish to Twitter

In this tutorial I will show you how to have your stories sent to the published page automatically Tweeted to Twitter using whatever Twitter account you want.

I have already shown you how to use Twitterfeed and Bit.ly to auto publish your published submissions, now lets cut out the middleman, Twitterfeed.

The following code was sent to us by Jorge from the good folks at Bierzeame.

Open your libs/link.php file and find:
function publish() {
if(!$this->read) $this->read_basic();
$this->published_date = time();

totals_adjust_count($this->status, -1);
totals_adjust_count('published', 1);

$this->status = 'published';
$this->store_basic();
}

change it to:
function publish() {
if(!$this->read) $this->read_basic();
$this->published_date = time();

totals_adjust_count($this->status, -1);
totals_adjust_count('published', 1);

$this->status = 'published';
$this->twitter();
$this->store_basic();
}

find:

function username() {

just before it add:
function twitter(){
global $db;

$id = $this->id;
if(!is_numeric($id)){return false;}
if(($link = $db->get_row("SELECT link_title FROM " . table_links . " WHERE link_id = $id"))) {

$message = $link->link_title;
$toshort = getmyFullurl("storyURL", $this->category_name(), urlencode($link->link_title_url), $id);

$bitly = "http://api.bit.ly/shorten?version=2.0.1&longUrl=".urlencode($toshort)."&login=BITLYUSERNAME&apiKey=YOURBITLYAPICODE&history=1";

$bitlyresponse = file_get_contents($bitly);

$jsonresponse = @json_decode($bitlyresponse,true);

$urlshort = $jsonresponse['results'][$toshort]['shortUrl'];

$messagelen = strlen($message);
$urllen = strlen($urlshort);
$totallen = $messagelen+$urllen+1;

if ($totallen>140){
$urllen = $urllen+4;
$message = substr($message,0,139-$urllen);
$message = $message."...";
}

$message = $message." ".$urlshort;

$args= 'status='.urlencode($message);

$twit = curl_init();
curl_setopt($twit, CURLOPT_URL, 'http://twitter.com/statuses/update.xml');
curl_setopt($twit, CURLOPT_VERBOSE, 0);
curl_setopt($twit, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($twit, CURLOPT_VERBOSE, 0); // no imprimir nada
curl_setopt($twit, CURLOPT_USERPWD, "TWITTERUSERNAME:TWITTERPASSWORD");
curl_setopt($twit, CURLOPT_POSTFIELDS, $args);
curl_setopt($twit, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($twit, CURLOPT_POST, 1);

$result = curl_exec($twit);
$http_status = curl_getinfo($twit, CURLINFO_HTTP_CODE);

curl_close($twit);

if( $http_status != "200" ) {
return false;
}

return true;
}
}

Save.

That’s it. Now every story that is sent to the published page will auto Tweet, giving your members another reason to submit.

Make sure you edit your Twitter login details and your Bit.ly details.

It will shorten the submission URL using Bit.ly and send the user when clicked on Twitter to your story page. This will also track how many clicks each one gets which you can check when you login to Bit.ly.

bitlyclicks

If you enjoyed this post, make sure you subscribe to my RSS feed!

  • Do you like Twitterfeed for sending blogs to Twitter? I am using the Ping.fm WordPress Plug-in but mainly because I can also see stats for it and it sends out to Diigo, Delicious, Plurk, LinkedIn and FB as well (both fan & personal pages).
  • When my tweets automatically publish on my blog, I’d like to be able to automatically add a prefix to the auto-generated blog post title. thanks for shearing..
  • I dont like bit.ly if my url is small enough for a tweet. Does this app post real urls or bit.ly?
  • It might be worth trying the facebook twitter app, if you don't mind every tweet going to facebook - not sure if this would work with auto blog tweets but then I don't have a blog to test it
  • Do you like Twitterfeed for sending blogs to Twitter? I am using the Ping.fm WordPress Plug-in but mainly because I can also see stats for it and it sends out to Diigo, Delicious, Plurk, LinkedIn and FB as well (both fan & personal pages).
  • I love Twitter Tools. When my tweets automatically publish on my blog, I’d like to be able to automatically add a prefix to the auto-generated blog post title. Is there a way to do that?
  • it any way to send the twits as the subject on an email using.
  • it any way to send the twits as the subject on an email using.
  • it any way to send the twits as the subject on an email using.
  • it any way to send the twits as the subject on an email using.
blog comments powered by Disqus