Writing a simple WordPress Plugin

There is two words to describe wordpress: “Simplicity” and “Flexibility”. You can do almost whatever you want with this application and its tons of free themes and plugins. This is a simple plugin example that include the steps I used to write my first plugin.
In this case I will replace the phrase “happy_face” inside the post by an image.

The Standards.

The Name

If you want to share your plugin with the community it has to be unique. No other plugin on the WordPress Plugin Directory can share this name.

In this example let’s use:

Happy Face Replacer

The Files

You need at least one “main file” .php with your principal functions. The name of this file could be derived from your plugin name. Besides you can use the others necessaries folders and .php, .js, .css files.
Our main file could be:


The Header

At the top of your main file you need to include this header.

Plugin Name: Happy Face Replacer
Plugin URI: http://www.pluginURI.com
Description: Plugin Description Here.
Version: 1.0
Author: Your Name
Author URI: http://www.yoursite.com

It will make your plugin look like this at the plugins panel:

The Hooks.

Beftore the version 1.2 to change the functionality of WordPress modifications (hacks) of the source code was needed. Now you can interact with the core of the application using the hooks.
The hooks intercepts the default wordpress behavior. For example, before wordpress shows the content of a post it check if there is a function (hook) registered that modified “the content”, if so the content is passed to this function before to print the final result.

There is two principal hooks:

Actions related to specific events like: add_category, get_header, get_footer.
To use the hook action you need to use this function:

add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );

On this link Plugin API/Action Reference you can find the ‘hook_name’ that you need to intercept.

The ‘priority’ is optional and specifies the order in which the functions associated with a particular action are executed (default: 10).

The last parameter defines how many arguments your function can accept (accepted_args = 1 by default).

Filters are function to modified the wordpress data like: the_excerpt, the_title, category_description. Visit Plugin API/Filter Reference.

If you want to ‘hook’ a function to a specific filter action you need this similar function.

add_filter ( 'hook_name', 'your_function_name', [priority], [accepted_args] );

In my example I will use a filter hook. So the main file could be something like this:

add_filter('the_content', 'happy_face_replacer');

function happy_face_replacer ($content) {
     $happy_face_image = "<img src='happy face image URL'>";
     return preg_replace('/happy_face/', $happy_face_image, $content);

The function “happy_face_replacer” takes the content of your post ($content) before to be printed and replace the phase “happy_face” by the image.

That’s it.

If you want to try this simple plugin just download the file and install it:

Here you have some useful links:
Writing a Plugin
Plugin API/Hooks 2.0.x
Plugin Resources
Custom Queries