Post Wordpress To Twitter Automatically, With Short URLs, No Plugin Required
Updated: Got rid of the Custom Metadata, used the modified date and the created date of the post to check if its a new post or an old one. Now you don’t have to worry about updating your old posts.
Updated: To Make the title text more Twitter friendly.
I been trying to cut down on the number of Plugins used. Firstly to make maintained easier, secondly to simplify and reduce to the bare minimum without using Plugins, thirdly to hone up my WordPress skills. The latest plugin I got rid of was the WP to Twitter Plugin. Initially I started off trying to modify how I wanted it to work. Basically I wanted to cut off Bit.ly as a URL shortner and use my own blog as a URL shortner to send people directly to my site and keep the traffic on my site without the need for a middle man. I figured this out after I saw how Amit Aggarwal was tweeting his links to his site.
Short URLs in WordPress without Bit.ly or URL Shortners
We all know that WordPress generates long Permalinks for your site, it automatically converts your titles into those long URLs called permalinks that you see. Example http://www.clazh.com/this_is_some_random_post, If you switch off Permalinks in WordPress you’ll see that links to your posts will be of the form http://www.clazh.com/?p=401 if you look at it you’ll notice the last bit has a number attached to it, each post has a unique ID associated to it. The trick is to make use of this ID to and create a URL for our posts, so when people click on this shortned link they’ll be automatically taken to the Permalink associated with that post its as simple as that.
A Simple WordPress To Twitter Function.
First we’ll write a really simple function that uses CURL to post to the Twitter API. Just copy paste it into your functions.php file in your WordPress theme directory. In case you don’t see the file in yours themes folder just create one and paste the code inside it. To check if CURL is enabled on your server you can paste this function into a blank PHP file and call the function like this twitterUpdate("some random post", "http://clazh.com", true); with some random inputs. Make sure you uncomment the last few lines where it says to uncomment to test for CURL support.
Note: In the rare case your host does not support CURL you can always make use of fsockopen for PHP 4 or the much easier stream_context_creat for PHP5
function twitterUpdate($postTitle, $postLink, $isNew)
{
// Enter Your Twitter ID Here
$username = 'Twitter_ID';
// Enter Your Twitter Password Here
$password = 'Password';
# 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 = 'New Post: ' . $postTitle;
else
$postTitle = 'Updated Post: ' . $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';
$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);
// Uncomment the lines below to check if
// CURL is enabled on your Web Server
// check for success or failure
/*
if (empty($buffer))
echo 'message';
else
echo 'success';
*/
}
WordPress Function that Triggers Post To Twitter
Next we’ll write a simple wordpress function that triggers whenever you update or publish a post, we will also added a custom metadata to the post check if its a new or old post. Copy paste this code below the twitterUpdate function in your functions.php themes file.
function postToTwitter($post_ID)
{
// Create your Short URL replace with your blog url
$postLink = ' http://clazh.com/?p=' . $post_ID;
// 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);
}
/* If You want to fire everytime You update
a post remove these comments around the else.
else
{
twitterUpdate($postTitle, $postLink, false);
}
*/
}
// Post to twitter when you publish or update a post
add_action('publish_post', 'postToTwitter');
So thats about it, just paste these two blocks of code into your functions.php and change the required parameter, you can play around with the code and modify it to change the message being posted to twitter.