Use This Source Code from wordpress to update in twitter

Insert this code in Function.php

<?php
function twitterUpdate($postTitle, $postLink, $isNew)
{
// Enter Your Twitter ID Here
$username = ‘eatwellin’;
// Enter Your Twitter Password Here
$password = ‘opensource2.0’;

# text into a twitter friendly text
$code_entities_match = array(‘–‘,'”‘,’!’,’@’,’#’,’$’,’%’,’^’,’&’,’*’,'(‘,’)’,’_’,’+’,'{‘,’}’,’|’,’:’,'”‘,'<‘,’>’,’?’,'[‘,’]’,’\\’,’;’,”‘”,’,’,’.’,’/’,’*’,’+’,’~’,’`’,’=’);
$code_entities_replace = array(‘-‘,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”);
$postTitle = str_replace($code_entities_match, $code_entities_replace, $postTitle);

// Check if New or Updated Post
if($isNew)
$postTitle = ‘www.eatwell.in  ‘ . $postTitle;
else
$postTitle = ‘www.eatwell.in ‘ . $postTitle;

// Calculate Twitter Msg and keep it under 140 Chars
if(strlen ($postTitle) > (140 – strlen ($postLink)))
$postTitle         = substr_replace($postTitle, ‘…’, (140 – 3 – strlen ($postLink)));

$message = $postTitle . $postLink;

// The twitter API address
$url = ‘http://twitter.com/statuses/update.xml&#8217;;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, “$url”);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$message”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

}
?>

<?php
function postToTwitter($post_ID)
{
// Create your Short URL replace with your blog url
$postLink = ‘ http://eatwell.in/wp/?p=&#8217; . $post_ID;
// encode the URL to fix Post to Twitter issues
$postLink = urlencode  ( $postLink ) ;
// Get the Post Object
$get_post_info     = get_post( $post_ID );
// Get the Post Title
$postTitle = $get_post_info->post_title;
// Get the Post date
$postDate         = date(‘U’, strtotime($get_post_info->post_date));
// Get the post Modified date
$postModified     = date(‘U’, strtotime($get_post_info->post_modified));

// Check if the post is new or modified
if($postModified == $postDate)
{
twitterUpdate($postTitle, $postLink, true);
}

}

// Post to twitter when you publish or update a post
add_action(‘publish_post’, ‘postToTwitter’);
?>

Advertisements

One thought on “Use This Source Code from wordpress to update in twitter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s