Archive for July, 2008

Object Oriented WP plugin structure

Friday, July 18th, 2008

When searching the web for WordPress plugin tutorials and introductions the vast majority utilizes procedural programming. However having recently discovered Object Oriented programming in PHP 5 i decided to take another approach to both plugins and themes.

When encapsulating plugins or parts of your themes in classes, you create a container for variable, constant and function names thus avoiding name clashes with the core or with other plugins. You can also encapsulate your class in a statement checking if the class already exists to ensure that in the rare case of a name collision your plugin will not initialize and crash the WordPress installation.

Here is a simple example

< ?php
// Plugin Name: Text Barking Dog

if (!class_exists('OurWpPluginDog')) { class OurWpPluginDog { function __construct() { $this->text = "wuff wuff"; } function bark() { echo ''; } } $dog = new OurWpPluginDog; add_action('wp_footer', array(&$dog, 'bark')); }

This plugin will insert “wuff wuff” as an html comment at the hook wp_footer. The __construct function runs when the class is constructed you can also place your actions and filters here, if you do you simply reference array(&$this … instead.

Object oriented programming encourages DRY – it simply makes it more convenient to reuse your code. As your plugin grows you will also find it easier to get an overview of the code.

Linux for the future

Wednesday, July 9th, 2008

About a year ago i bought a Macbook Pro and made the change to Mac OS X from Windows, and for a while i became a true Mac-Zealot. Then as now i was enthusiastic about open source software and hell i discovered a lot of small and nifty applications for OS X. However more often than not i found that the best Open Source applications was made natively for Linux and then ported to Mac, and the best games were made natively for windows and often not ported at all. At first i was perplexed that software manufacturers did not develop for Mac. But as i became increasingly aware of Apples company structure, it wasn’t startling. In How Apple Got Everything Right By Doing Everything Wrong Wired describes the Apple company as the Evil Genius in vivid detail. I won’t go into further detail on the article but i truly recommend you read it, especially if you are a dedicated Apple fan the article can put things in perspective.

Motivated by moral reasons i finally installed Ubuntu on my Macbook Pro. The change did not involve major complications. In fact the synaptic package manager puts more order into your computer than any drag and drop interface and gives you access to an ocean of amazing Open Source utilities making ubuntu a good choice for both the casual and the professional user. Unless you are a hardcore gamer ubuntu will most likely more than suit your needs.