0

My First Static WordPress Plugin

After almost 3 hours of researches and trials, I finally finished the first one-page WordPress plugin called “identi.ca reminder”. As the name suggests, it’s used for reminding me to post an update whenever I added or updated a blog post. Since identi.ca also posts updates automatically to Twitter and Facebook, I only need to update the status for my identi.ca account. And because the lack of the good identi.ca-related WordPress plugins, I decided to manually update the status once a blog post is added or updated.

This was the first time I began to learn WordPress plugin. Thanks to the detailed documentation on the WordPress Codex wiki, I was able to learn and create the plugin at the same time. Here is the whole source code for my basic plugin (also includes very detailed comments):

<?php
/*
Plugin Name: identi.ca Reminder
Plugin URI: http://wordpress.org/#
Description: This is my first plugin simply remind you to post an update on identi.ca after a blog post is published/updated
Author: Robby Chen
Version: 1
Author URI: http://blog2.robbychen.com/
*/

    // Function to output the reminder
function reminderID($messages) {
    $reminder = array();    // An empty array to store the duplication of the messages array
    $others = array();      // An empty array to store the arrays for post types other than messages['post']
    foreach ($messages['post'] as $num=>$message) {
        $message .= "<br /><br />Make sure to update your <a href='http://www.identi.ca/robbychen' target='_blank'>identi.ca</a> status.";
        if ($num == 0) {
            $message = "";  // The first element in the messages['post'] array is always empty
        }
        array_push($reminder, $message);    // Insert/copy the modified message value to the last element of reminder array
    }
    $reminder = array("post"=>$reminder);   // Apply the post key to the first dimension of messages array
    foreach ($messages as $type=>$message) {
        if ($type == "post") {  // Skip the post key since it is already defined
            continue;
        }
        $others[$type] = $message; // Since message is an array, this statement forms 2D array
    }
    $reminder = array_merge($reminder, $others);    // Merge the arrays in the others array with reminder array in order to form an appropriate format for messages array
    return $reminder;
}
    // Print the above message when a blog post is submitted or updated
add_filter("post_updated_messages", "reminderID");
?>

Note that when I wrote and tested this plugin, the “messages['post']” array cannot be changed partially. The only way I can change the value is to copy the modified value to a new array, then make the structure of the new array the same as messages’. For instance, I tried to do the following in the code above:

$messages = array('post'=>array(), 'page'=>array(), 'custom_types'=>array(), ...);
$reminder = array('post'=>array(), 'page'=>array(), 'custom_types'=>array(), ...);

It seems that without this format, the add_filter function would not be executed. As the above example shows, it just applies to the “post” post type, although I have not tested it with custom post type yet.

You can download the source file here.

Below are the resources I used to help me for creating this plugin:

2

My New Mini-blog (Updated)

(UPDATE 06/28/10) I’m closing down the mini-blog because it gives me a huge productivity lag by posting the same content onto that blog and Twitter/identica. Tumblr will automatically post the new articles to Twitter and Facebook, but not to identica. Please follow me on Twitter, Identica, and/or Facebook instead.

My new blog, mblog.robbychen.com, is a Tumblr powered blog. My goal of new blog is to post links, quotes, and videos I found around the web that are related to GNU/Linux and web development, as it corresponds to this blog. In fact, I created this Tumblr account long before this WordPress-based blog was created. At that time, I had no idea what to post to the blog since I didn’t have any specific topic. I remembered this account that I had been forgotten long ago when I stumbled across this article. It basically states that Tumblr could replace Twitter. I decided to reuse my Tumblr blog as a mini (alternative) blog to this blog. Because this mini blog is hosted directly at Tumblr, I had to change the DNS settings of mblog.robbychen.com to the specified Tumblr IP. I hope to have a backup feature on Tumblr to allow me to backup my blog content. The mini blog right now has only one post titled First Post. It describes the purpose of the blog. I will post new posts often when I come across interesting articles as well as on my Twitter and Identi.ca accounts.