Technobabbles I try to sound like I know what I'm talking about. Don't be fooled.

22Jan/102

My First WordPress Plugin Patch: Wibiya Toolbar

The beauty of using WordPress instead of Blogger is, in a nutshell, the freedom that comes with using an open system instead of a closed one. Under Blogger, I had very little freedom to extend the platform. Everything I could do had to be added by someone from Google, with the exception of a few JavaScript- and Flash-based sidebar widgets. Under WordPress, I have access to literally thousands of open-source plugins to modify, extend, and replace the functionality of the site.

The beauty of using this open system is that if something doesn't work the way I want it to, I am free to simply change it; every plugin is editable from within WordPress' administrative back-end, and the core code is also hackable (though I don't like messing with it because upgrades will break changes). When I discovered that the Wibiya Toolbar (or Wibar) was showing on the mobile version of the website (which is generated by MobilePress, another good plugin), I simply looked in the source of Wibiya's plugin to see how the JavaScript for the toolbar was being inserted.

I found that instead of using the wp_enqueue_script() function as WordPress plugin developers are supposed to do, the Wibiya developers simply used echo to output a <script> tag. That explained why only the JavaScript for the Wibar was loading on the mobile site. Rewriting the plugin to use the official WordPress script-injection method solved the problem.

The original relevant code was:

add_action('get_footer', 'filter_footer');
add_action('admin_menu', 'wibiya_config_page');

function filter_footer() {
    $wibiya_toolbarid = get_option('WibiyaToolbarID');
    $wibiya_enabled = get_option('WibiyaToolbarEN');

    if ($wibiya_toolbarid != '' and $wibiya_enabled) {
        echo '<script src="http://cdn.wibiya.com/Loaders/Loader_'.$wibiya_toolbarid.'.js" type="text/javascript"></script>';
    }
}

I rewrote it just a little bit. My changes were pretty trivial, really. I changed the function name to be namespaced (including the plugin name) just so it'd be less likely to conflict with another plugin. I also switched actions to enable wp_enqueue_script() to work (get_footer is called too late) and added a check—if( !is_admin() )—to keep the toolbar off of admin pages, preserving the original behavior of the function (and the sanity of anyone using the modifications).

In short, wp_enqueue_script() takes five parameters, three of which are optional. The first two are the $handle and the $src, which specify a name for the script and its source address. Then come $deps (dependencies; Wibiya has none, so set to false), $ver (version; also false because it's irrelevant), and $in_footer (set to true because the toolbar should be inserted above the </body> tag).

So with my changes, the code block above becomes:

if( !is_admin() ) {
    add_action('wp_print_scripts', 'wibiya_filter_footer');
}
add_action('admin_menu', 'wibiya_config_page');

function wibiya_filter_footer() {
    $wibiya_toolbarid = get_option('WibiyaToolbarID');
    $wibiya_enabled = get_option('WibiyaToolbarEN');

    if ($wibiya_toolbarid != '' and $wibiya_enabled) {
        wp_enqueue_script( 'wibiyabar', 'http://cdn.wibiya.com/Loaders/Loader_'.$wibiya_toolbarid.'.js', false, false, true );
    }
}

Following some light testing, I submitted an idea to the Wibiya feedback forum, where my update is currently being reviewed by the company. Hopefully, the change will be included in a future version of the Wibiya WordPress plugin. After all, it changes nothing for Wibiya but works wonders for compatibility with other plugins.

I'll just bet that this won't be the last time I'll send a patch to a plugin developer. I enjoy reporting bugs and coming up with fixes too much to not do it. ;)

17Mar/080

GIMP Script to Make SW Robotics Thumbs

After making a whole bunch of thumbnail images for the member and mentor profiles on the Southwest Robotics site this past season, I finally decided last night to automate the process with a GIMP script, in preparation for next year's new people.

So, what does it do? It takes the image it is run on, scales it (gimp-image-scale) to 120px x 90px, and utilizes script-fu-drop-shadow to add a correctly-positioned drop-shadow. (Correctly is, in this case, the same as all the other images I did manually.)

It's a rather simple script; here's the code:

; Script-Fu script to automatically;   resize and drop-shadow files for;   SWR profile shots; IMPORTANT!; Image must be EXACTLY 4:3 aspect;   ratio or it will be distorted.; This script does not measure the;   image or do any cropping; it is;   up to you to crop the image properly.

(define (swr-make-profile-thumb img drawable)(gimp-image-scale img 120 90)(script-fu-drop-shadow img drawable 4 4 8 '(0 0 0) 80 TRUE))

(script-fu-register "swr-make-profile-thumb""Make SWR Profile Thumb""Scales the image to 120x90 and adds an appropriate drop-shadow""Voyagerfan5761""Voyagerfan5761""March 17, 2008""RGB*, GRAY*"SF-IMAGE "Image" 0SF-DRAWABLE "Layer" 0)(script-fu-menu-register "swr-make-profile-thumb""<Image>/Script-Fu/SWR")

Note: The original has my real name and an email address, but I have stripped them out for purposes of posting this in view of Web crawlers.

Input images must be in 4:3 aspect ratio to avoid distortion. I currently have no validation for aspect ratio, nor am I certain that it's even possible to check such things, but if it is I will add it at some point. As can be seen from two of my updates last night on Twitter, I only just learned Script-Fu Scheme less than 10 hours ago.

The only real problem I had was developing the script-fu-drop-shadow line, which kept choking when I tried to set an interactivity mode. I eventually figured out that that parameter wasn't required in 2.4 (or some other weird problem; I'm still not sure).

It's obviously not much, just a few basic lines of the stripped-down Scheme that GIMP uses. Nevertheless, if you use my script as a base for a released script of your own, I would appreciate an acknowledgment. A link back here along the lines of, "Based on a short script by Voyagerfan5761" would be sufficient (note the link on "Voyagerfan5761").

And of course, I'd appreciate any and all comments on this script, be they pointing out a better way to do things, something I missed, a suggestion for validating the aspect ratio (hey, it never hurts to ask). Or even a note that you based a script of your own off of mine. That's highly unlikely, considering I didn't even copy any code examples for this one. (OK, I lifted and modified the registration function from GIMP tutorial page at GIMP's website, but that's all.)