Writting a WordPress Plugin

In my post ‘Changing the WordPress Search Form to HTML5‘ I mentioned it could be a made into a plugin. Today we will have a quick look at writing a plugin. We will be building a useless plugin which does basic word filtering, which will change ‘the’ to ‘da’, though not in any smart way. The plugin will be like the ‘egg’ to ‘duck’ word filter from 4Chan’s Moot which resulted in the DuckRoll MeMe.

Getting started:

  • Create a folder into your WordPress Plugins folder e.g. ‘TheToDa’
  • Create a PHP file in the new folder e.g. TheToDa.php
  • Put the key information about your plugin in the top of your file

The header

[cc lang=”PHP”]

/*

Plugin Name: The to Da – Word Filter
Plugin URI: https://createmycomau.files.wordpress.com/2010/12/thetoda.zip
Description: A silly little plugin which will change all the words ‘the’ to ‘da’
Version: 1.0
Author: Dale Hurley
Author URI: http://createmy.com.au
License: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

Copyright 2010 Dale Hurley (email : dale@createmy.com.au)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[/cc]

  • Plugin Name is the name WordPress will show as the name
  • Plugin URI is where the Plugin can be downloaded from
  • Description is a the description used for the plugin which can include some basic HTML
  • Version allows to inform people of the version for upgrades
  • Author and URI creates a link to you
  • License is a URI and the license agreement for the plugin which in this case GPL is being used

Vital, use classes for protection

A lot of plugin tutorials skip over the vital step of wrapping your plugin in a class. By using a class you reduce the chance of code conflict with other plugins. For instance I have seen the function ‘my_admin_init’ used in multiple plugins which will cause WordPress to crash. First we will check that there is no other class with the same name, if there isn’t we will create our class.
[cc lang=”PHP”]if (!class_exists(“the_to_da”))//check that there is no other classes like this
{
class the_to_da //it takes class to use classes
{

}
}//end the check class exists[/cc]

Next we will add our code to create the object using our class.

[cc lang=”PHP”]if (!class_exists(“the_to_da”))//check that there is no other classes like this
{
class the_to_da //it takes class to use classes
{

}
$the_to_da=new the_to_da;//create the new object
}//end the check class exists[/cc]

The functions

Now we will add the function with the same name as the class, this will be executed when the object is called. We will also add our reusable word replace function.

[cc lang=”PHP”]if (!class_exists(“the_to_da”))//check that there is no other classes like this
{
class the_to_da //it takes class to use classes
{
function the_to_da()//construct
{

}

function word_replace($str=”, $from=’the’, $to=’da’)//portable
{

}
}
$the_to_da=new the_to_da;//create the new object
}//end the check class exists[/cc]

The filters

We will add the filters to title, excerpt and content using the add_filter hook. As we are using a class we will use an array to point to the function.

[cc lang=”PHP”] function the_to_da()//construct
{
add_filter(‘the_title’, array(&$this, ‘word_replace’));//add a filter
add_filter(‘the_excerpt’, array(&$this, ‘word_replace’));//add a filter
add_filter(‘the_content’, array(&$this, ‘word_replace’));//add a filter
}[/cc]

Finally we will create our word replace function which is executed any time a title, excerpt or content function is called. This is a dumb word replace, and you would normally use a preg_replace.

[cc lang=”PHP”] function word_replace($str=”, $from=’the’, $to=’da’)//portable
{
return str_replace($from, $to, $str);//replace $from with $to
}[/cc]

Our final code

[cc lang=”PHP”]
/*
Plugin Name: The to Da – Word Filter
Plugin URI: hhttp://createmy.com.au/quick-tip-changing-the-wordpress-search-form-to-html5/
Description: A silly little plugin which will change all the words ‘the’ to ‘da’
Version: 1.0
Author: Dale Hurley
Author URI: http://createmy.com.au
License: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

Copyright 2010 Dale Hurley (email : dale@createmy.com.au)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

if (!class_exists(“the_to_da”))//check that there is no other classes like this
{
class the_to_da //it takes class to use classes
{
function the_to_da()//construct
{
add_filter(‘the_title’, array(&$this, ‘word_replace’));//add a filter
add_filter(‘the_excerpt’, array(&$this, ‘word_replace’));//add a filter
add_filter(‘the_content’, array(&$this, ‘word_replace’));//add a filter
}

function word_replace($str=”, $from=’the’, $to=’da’)//portable
{
return str_replace($from, $to, $str);//replace $from with $to
}
}
$the_to_da=new the_to_da;//create the new object
}//end the check class exists[/cc]

Conclusion

Creating a plugin is really simple. By following some simple practices you will be pumping out plugins daily.

Two final notes is that you should not close you PHP as any white-space after the close will cause WordPress to die. Second, if you are going to publish your plugin you need to include a readme file.

Check out http://codex.wordpress.org/Writing_a_Plugin

Quick Tip – Google Font Directory

Sick of the 16 Web safe font? Well Google has a solution.

Google Font Directory is a list of additional fonts you can add to your website available for use, for free, and really simply. Check out Toolnames which is using the GFD.

To add a new font you just paste the embed code for the font in between your head tags, before your style-sheet and then you are able to use the additional font in your site while knowing it will work in Safari, FireFox, IE6+ and Chrome, on Windows and Mac (maybe Linux).

For instance I want to use Molengo I paste the below into my head tags

And then am free to use Molengo in my Font-Family calls in CSS.


h1 { font-family: 'Molengo', arial, serif; }

Quick Tip – Simple HTML5 Video

HTML5 is awesome. It extends the DOM to be ready for the future once broadband is wide enough to replace your old broadcast TV in full HD.

Video is obviously the most exciting element. Now you can directly interact with the video in the DOM which can mean you can make a fully, easily to update scripted site which does not rely on plugins.

There is a few things to note about HTML5 video:

  1. There is no standard video format so you will need to encode your videos using both MP4 and OGG Theora. Google has WebM which may become the standard. Use Handbrake to encode the MP4 version and ffmpegtotheora to encode the OGG. Google this, it will take you less then 5 minutes to encode your video in the formats.
  2. You will want to use a fall-back for older browsers. I like Flowplayer which is a Flash video player
  3. Progressively enhance your site. If you want to do some cool stuff with the video ensure that you still have a working site for those using a 9 years old browser.

If we want to have a video with is 843×480 which preloads, plays once it is buffered and contains controls we will use the below

<video width="843" height="480" preload autoplay controls>
 <source src="/video.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
 <source src="/video.ogv" type='video/ogg; codecs="theora, vorbis"'>
</video>

That is exactly how simple HTML5 video can be. Of course you may want to add poster or change the settings.

Since we want fallback for IE we will use Flowplayer by embedding it after the other sources. This is a cool thing about the element is that it has fallback.

<video width="843" height="480" preload autoplay controls>
 <source src="/video.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
 <source src="/video.ogv" type='video/ogg; codecs="theora, vorbis"'>
 
</video>

If you want a real in depth look at HTML5 video I recommend you read:

365 Days of Free PSDs

365psd is a simple concept, give away your old PSDs. Basically designers create amazing designs which clients do not like. So instead of discarding them to never see life the designers give the designs away for free.

Of course you cannot resell the designs, though who would?

The designs are simple great.

How do I find the largest top 10 files and directories on a Linux / UNIX / BSD filesystem?

The other day I got a warning message that my server’s file system was full. I was like ‘WTF? this is a new server image’. So I used a quick command in terminal (below) and found that the error log was 19GB in 404 errors (WHOOPS!).

# du -a /var | sort -n -r | head -n 10

via How do I find the largest top 10 files and directories on a Linux / UNIX / BSD filesystem?.

Also check out the new toolnames.com I rebuilt it from the ground up making it much simpler and 10 times faster.