New Plugin: Speak Pirate

Argh mateys! Shiver me timbers! Sometimes it feels good to step out of your normal coding routine and do something a little different.

Yesterday, @arod2634 posted this tweet:

One of the APIs listed was a pirate translation api that would take English and turn it into the equivalent as a pirate would say it. What better way to put it to use then make a WordPress plugin out of it: Speak Pirate.

This new plugin be hot off yonder press and working hard to convert ye plain text into pirate jargon. Simply use ye new shortcode [speak_pirate] around ye text you want translated and it shall be done.

[speak_pirate]Text to translate goes here[/speak_pirate]

Enjoy, ye land lubbers!

Hungry For More Information?

Here is the commented code:

Object-Oriented Plugins for WordPress

Making your WordPress plugins (and themes for that matter) object-oriented definitely has it’s benefits; most notably not running into naming conflicts with other functions. You are essentially creating a new workspace for your functions to live and could have the same function name as found in the WordPress core.

How to Get Started?

Plugin Declaration

The first part of the code is your plugin declaration in the commented out lines. This is required by WordPress!

Class Declaration

You will have to create a unique class name for your plugin. Once you’ve figured out what to call it, you’ll want to add a __construct() function to it. This is typically where I will put all of my actions and filters, along with any special initialization code for my plugin.

Actions and Filters

This line may look a little different from normal add_action() and add_filter() calls. For our second parameter, instead of passing a string for our function name, we are passing an array containing our class object, $this, and the corresponding function in our class, admin_init. The function name is arbitrary; it could be erics_super_awesome_admin_function for all WordPress cares. I typically name my function the same thing as the action or filter for clarity on my end. It seems easier to find things that way to me, and I know precisely when it’s going to happen.

Instance of Class

This is a major step. If you forget this, your plugin (or theme) won’t work. We have to create an instance of our class for the code to run. I’ve created a new variable named $my_object_oriented_plugin that contains an instance of the class.

Questions?

Fire away in the comments.

I would highly recommend that anyone doing WordPress plugin or theme development look into writing object-oriented code. It’s not as hard or scary as people think it is.

Allowing Editors to Clear the Cache using Quick Cache

I started using Quick Cache on a site recently and some of the publishers (setup as editors) wanted to be able to clear the cache manually to ensure their posts went live right away. So I wrote a quick script to handle that. I’ll probably release a plugin for this eventually, but for now…here’s the code:

function check_publish(){
    global $current_user, $post;
        
    $capability = 'publish_';
    // Check current post type...if we have one available
    if($post->post_type != ''){
        // Set the capability we are looking for to publish_s
        $capability .= $post->post_type . 's';
    } else {
        // Set the default capability to publish_posts
        $capability .= 'posts';
    }
    
    // Check if the user has rights to publish a post
    return current_user_can($capability);
}
add_filter('ws_plugin__qcache_ms_user_can_see_admin_header_controls', 'check_publish');

This will include the Clear Cache button in the admin section of WordPress.

Update: I’ve released this plugin on wordpress.org.

Piwik Analytics

Has anyone messed with Piwik Analytics? I just got it setup on thejudens.com yesterday, and so far it seems pretty slick. The one thing I like most about it is that the data is mine! I don’t have to share it with google! Right now we have both google analytics and piwik running on the server. I’m going to let them run and see how they compare with each other. I’m just kind of curious how close the results will be with the other. I’ll report back later and let everyone know.

In the meantime, you better go check out Piwik. It runs on php/mysql and you can most likely install it on your hosting account.