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:

happy_face_replacer.php

The Header

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

<?php
/*
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

14 thoughts on “Writing a simple WordPress Plugin”

  1. I’m not sure exactly why but this weblog is loading extremely slow for me.
    Is anyone else having this problem or is it a issue on
    my end? I’ll check back later on and see if the problem still
    exists.

  2. I don’t know whether it’s just me or if everyone else encountering
    problemswith your site. It seems like some of the text on yourr content
    are running off the screen. Can somebody else please provide feedback and
    let me know if this is happening to thnem too?
    This might be a issue ith my browser because I’ve had this happen before.
    Cheers

    Here is my weblog; supplements cheap

  3. This is a very good tutorial. I have heard of such plugin and a friend of mine have tried to show off using that type of plugin but never told me where I can find the information on how to do my own. I will use this tutorial to help me create this plugin and use it on my sites.

  4. Very interesting plugin tutorial i have used it in changing specific keywords on my site to automatically display a link pointing to my site. Thanks for this tut!

  5. http://www.superdirectorysubmitter.com/

    finally the perfect solution pour webmasters:

    super directory submitter Website Submitter
    is a program designed to submit your web page to thousands 20 000
    of higher Quality directories. It will save you a lot of time, effort and money since its fully semi-automated. You just enter some information about your web page and the program does the rest. With SDS software you will never need to search for places to advertise again.

  6. This sort of worked for me. I kept getting “couldn’t modify header” errors. I was able to adapt this and add it to my functions.php so it was a permanent fixture of my theme though.

  7. Thank you for this well written tutorial. I have been wanting to learn more about PHP and this gives me a nice start. You have whetted my thirst for knowledge here. Now I want to learn the language even more. I suppose I’d better learn HTML first though eh?:)

Leave a Reply

Your email address will not be published. Required fields are marked *