Twitter To WordPress Widget & Post To Twitter Function, Short URLs Without Plugins

I been looking at ways to cut down on the number of plugins I use with WordPress. While WordPress does have a inbuilt mechanism to update plugins, I find it a hassle to keep them updated and sometimes I really don’t require most of the other functionality a wordPress plugin provides.

WordPress Short URLs In Twitter

Here is a small trick that is very useful in helping you maintain the link juice to your site and also to helps in avoid sending people to a third party URL shortner. Its very common to use a link shortner to reduce the number of characters a link takes up when you tweet.

If you care about SEO you will most probably be using WordPress Permalinks in the form of http://somesite.com/some_random_post/. You will notice, if Permalinks are not enabled, then by default your site links are in the form of http://somesite.com/?p=44 . The trick is to get the post id of whatever link you want to post on twitter and post it in the form of http://somesite.com/?p=44 this link will automatically redirect to your actual blog post as this is an inbuilt feature in wordpress called canonical URLs, not only does this reduces the character count but it also helps your twitter followers to go to your site directly without using any third party URL shortners.

Automatically Post Latest WordPress Articles To Twitter

We will use the tip I explained above to write a wordpress theme function that automatically posts a tweet to your twitter account whenever you write a new post or update any of your posts or articles. Before we use this function we’ll check if CURL is enabled on your server. All you need to do is to paste the function given below into a blank PHP file and call the function with some random inputs, somewhere on the same page like this twitterUpdate("some random post", "http://zetalight.com", true); . Make sure you uncomment the last few lines where it says to uncomment to test for CURL support. In the rare case your web host does not support CURL you will have to make use of fsockopen for PHP 4[1] or the much easier stream_context_creat for PHP5[2]. Next make sure you have file called functions.php in your theme directory. I put comments before every line explain what is happening in the function. There are basically two functions one is the twitterUpdate function which take care of posting to twitter, the other one is postToTwitter function which is a function that calls the twitterUpdate only when a new post is created or updated. copy paste both these functions in your functions.php file and enter your twitter UserName and PassWord in the appropriate places.

Twitter To WordPress Widget

Here is another simple JavaScript code to display your latest twitter tweets in your sidebar, you can either use it as a widget or just paste it in your sidebar. Since its JavaScript based it doesn’t cause any load on your WordPress site and loads you latest tweets very fast. Not only does it get your latest tweets, but converts the links into hyperlinks and twitter profiles. You can copy paste this code into a text/html widget to get it working. make sure there is a DIV with ID="twitter" somewhere on the page, you can load the JavaScript separately in your footer if you want.