So I just got my this blog set up to send a tweet to twitter whenever I make a post. I love wordpress functions and API’s! Also I set the twitter tools plugin to make a weekly digest of my tweets in a post! How cool is that? Really cool. A few days ago I set up a custom field to hold the tr.im url so that whenever someone clicks the “Tweet this” button on the bottom of my post it adds the tr.im url instead of a permalink for a cleaner look and more space for the post title. With Twitter Tools and the custom tr.im plugin I will be able to automagically fill in a custom field with the tr.im url so I don’t have to do it manually.
Update: For some reason I cannot get the Twitter Tools plugin to make a custom field with the trimmed url, any suggestions?
Update #2: The trimmed url that was generated is not tied to my account, maybe I should use my API key instead of the username & password?
Update #3: I fixed the code below to show how to get the trimmed url to work with your accounts. I changed "&username=" to "&username=
Tools used:
And this blog post to tie it all together: How to use tr.im with twitter tools.
To do it in an easy step by step fashion:
1. Install the Twitter Tools plugin and set it up.
2. add the following somewhere in your functions.php file in your themes folder. Make sure to change the username and password to your tr.im account.
// Add hook for Twitter Tools URL shortening filter
add_filter('tweet_blog_post_url','makeTrimURL');
function makeTrimURL($myURL) {
// Accepts a WordPress post URL
// Returns a tr.im URL or original URL if unsuccesful
// Change USERNAME and PASSWORD to your tr.im login
$trimUsername = "USERNAME";
$trimPassword = "PASSWORD";
// Construct the tr.im query string
$trimQuery = "http://api.tr.im/api/trim_url.json?url=" . $myURL;
$trimQuery = $trimQuery . "&username=" . $trimUsername;
$trimQuery = $trimQuery . "&password=" . $trimPassword;
// Query tr.im and decode returned JSON string
$trimJSON = json_decode(file_get_contents($trimQuery));
// If everything went OK we'll return the tr.im URL
// Otherwise we'll return the original URL
if ($trimJSON->{'status'}->{'result'} == 'OK') {
$trimURL = $trimJSON->{'url'};
} else $trimURL = $myURL;
// And we're done!
return $trimURL;
} |