Auto-tweet from Wordpress with Petite URLs
I wanted to automatically update the @DefProc twitter feed whenever I publish a post from this blog, and I suddenly found I was a WordPress plugin developer!
A quick search at WordPress.org’s plugins found a fair few that would send a tweet when a post was published or updated, and would add a short url from one of a selection of sources. Unfortunately, I didn’t find one I could use to send the internally generated short urls I use from the la_petite_url plugin I discussed last week: Combating Link Rot on Short URLs. So I was left to roll my own.
TwitterUpdater was a great starting point, with only 2 php files, its a very small plugin, so it was the easiest to get a handle on; and it seems to have a great tradition of being updated by whoever needed it to do something something extra…
The last update to bring it to v.2.10 was from Jordan Trask (@comm) who, amongst other improvements, rewrote the url-shortening as a function, allowing the addition of more shorteners at a later date. Because of this, including the la_petite_url was fairly straight forward.
I adding an option to retrieve the url if the la_petite_url plugin is installed, that will to revert back to the default if the plugin has been subsequently deactivated, into the twitter_updater.php file.
` else if ($urlmethod == ‘4’) { // Added support for la_petite_url shortener plugin
if(function_exists(‘get_la_petite_url_permalink’)) {
$turl = get_la_petite_url_permalink($post_ID); // return short link from la_petite_url
} else {
// Don’t want things to fail completely if la_petite_url gets deactivated, so reset to default and continue
update_option(‘url-method’, ‘2’);
$url = “http://tinyurl.com/api-create.php?url=”.$link;
if ($shortmethod == ‘1’) {
$turl = file_get_contents_curl($url);
} else {
$turl = file_get_contents($url);
}
}
}
`
All that was required to have the function work, was to pass an extra variable $post_ID
in each of the three possible function calls.
The additional option was added to twitter_updater_manager.php to allow selection of la_petite_url as the shortened url.
<?php if(function_exists('get_la_petite_url_permalink')) { // If activated, allow selection of la_petite_url plugin as the shortener ?>
<p>
<input type="radio" name="url-method" id="url-method" value="4" <?php vc_checkRadio('url-method','4') ?> />
<label for="url-method">le;_petite_url</label>
</p>
<?php } ?>
The default format of the tweets send by TwitterUpdater is of the form: “Published a new post: #title#” with the option to append “@(url)” via a checkbox. The first part could be changed on the plugin manager page, but only way to change the format of the additional string was through editing the php files in three places, to alter:
$thisposttitle = $thisposttitle . ' @' . $tinyurl;
Becuse Twitter uses the @ symbol to address users, it didn’t seem quite appropriate to use it to prefix a link in twitter. I can understand why it is included in the code, the @ identifies its suffix as a target, but I prefer the use of a colon in this instance.
The code already includes a #title# placeholder that is replaced when the tweet is generated, so I decided change the way url inclusion is handled, and add a string replace on a new #url# placeholder.
So the tweet is generated as:
$sentence = get_option('newpost-published-text');
$tinyurl = get_tinyurl(get_option('short-method'),get_option('url-method'),$thispostlink,$post_ID);
$sentence = str_replace ( '#title#', $thisposttitle, $sentence);
$sentence = str_replace ( '#url#', $tinyurl, $sentence);
This way, I can specify the original format in the manager with “Published a new post: #title# @#url#”, or my preferred format as “New Post - #title#: #url#” without having to change the source files.
The changes seemed like enough of an addition to add a version change, taking TwitterUpdater to v.2.11. I’ve created a Git repository for the plugin, at Github, where you can download v.2.10 or v.2.11 directly.
Addendum:
I was happy that the new code worked, and added my tweets as I expected and was ready to leave it at that. Then I found www.countdowntooauth.com, the countdown to when Twitter will stop allowing API calls via basic authentication. This means that in less than two weeks from the date of publishing, the newly updated v.2.11 of TwitterUpdater plugin will stop working.
Because this plugin is the only one I’ve found that can post using the la_petite_urls, I’ll be updating it to work with OAuth as “TweetUpdater”. More next week…