WordPress plugins – Front Controller

WordPress supports extensions through plugins. Plugins integrate into the core of WordPress with hooks. These hooks are executed at places in the WordPress code. An example of this is when a web page of posts is displayed, when a user is created or when a post is deleted.

There is an action hook that is called “init” that is executed the moment before the theme gets loaded and posts gets presented. When this hook is executed all plugins are loaded and options from the database are retrieved from the database, and therefore is this hook very suitable for what I am about to show you here.

I often need to call functions available from my plugin from a JavaScript, also known as Ajax, or from an HTML form. As an example I will describe how to create a filter that lets the user use a filter, such that only posts that matches this filter will be shown. I have written a plugin that makes use of this technique. The plugin is not publicly available, but I will show you how it can be done.

This filter sets a cookie in the users web browser. The user will then have the filter active until the users changes to some other filter. I will show you two examples, first where I use a regular HTML link to change to a different filter, then I will show you how Ajax can be used to retrieve a list of available filters. Filters can be placed in levels relative to each other, where a filter will have a parent filter. Think of it as categories in WordPress.

The link to change a filter can look something like this:

<a href="http://example.com/?myplugin=set&filter=sport">Sport</a>

When i click on this link I need my plugin to check for those parameters I send. In my examples I call my plugin for “myplugin”, although thats not the real name of it, but then again it’s only an example ;) . In the URL in the example above I have used the code word “myplugin”, the same as the name of the plugin. This is to reduce the possibility of name collisions with other plugins. In the PHP code of the plugin I have connected a function to the “init” hook that checks if the code word has been given as a parameter. It will then look for a suitable action to execute based on the value of the code word.

The following example shows the PHP code that accepts both a request to set a filter and to return a list of all filters that has a certain parent. If the first test matches a filter gets set and WordPress continues to load the page. In the second test you can see that the script dies the moment after it prints the list of requested filters. That is actually desired because we are going to make an asynchronous JavaScript call to this function that will want only this list, not a normal WordPress page. With the JavaScript call we are not going to reload the page, but only update a small part of the page, whether it is a <select> list, a list of checkbox’ or some other action decided by the JavaScript.



$myplugin = new MyPlugin();

add_action('init', array(&$myplugin, 'check_action'));

//

// The main class of the plugin

class MyPlugin {

    function check_action() {

        $action = _REQUEST['myplugin'];

        switch($action) {

            case 'set':

                $this->set_filter($_REQUEST['filter']);

                break;

            case 'list':

                $list = $this->get_list($_REQUEST['parent']);

                echo $list;

                exit;

        }

    }

    // Set a new filter

    function set_filter($filter = '') {

        // use setcookie to set the filter in the web browsers cookie

    }

    // Returns a list of filters with a given parent

    function get_list($parent = '') {

        // lookup all the filters with the parent given as a parameter

    }

}

I will not go into details of how JavaScript can be used to make asynchronous calls. It would be too big and too many details to dig into in this article. There are many good resources on the Internet about Ajax, and Google knows about most of them. It can be a good thing to check out Cross-Browser XMLHttpRequest that makes it a lot easier to use Ajax without all the pain of different techniques and implementations between web browsers. Prototype and jQuery are two very powerful JavaScript libraries that are worth to have a look at as well. Beware that those two are per default not compatible with each other, use either Prototype or jQuery if you are not already familiar with them.

This article is a translation of my norwegian article from march 8.

1 comment

  1. WordPress plugins - Directory Structure | Datafeel

Leave a reply

You must be logged in to post a comment.