WordPress, Custom Filters And Decoupling

I’m sure you are aware that having loosely coupled classes in your WordPress plugins is important for maintenance and bug fixing.

If you are not sure what coupling is, this is when you have two software modules which are closely linked together. Where you have tightly coupled modules a change in one module has a good chance of causing a side effect in the other module.

As I find a picture says a thousand words I think a quick example is probably in order.

Say I have a plugin class which creates a widget which you can use to put stock quotes on your front end.

Now my Stock_Quote_Class will get created and called from my stock-quote-widget-plugin.php which is my plugin file to be called by WordPress.

In my Stock_Quote_Class I want to handle all data persistence and any business logic that will need to run. I have a separate file, stock-quote-view.php, for displaying data on the front page and I have a separate class for the WordPress widget.

As I said above I want the Stock_Quote_Class to handle most of the data persistence but some of the data will also need to be passed to the widget for customisation purposes.

I could create the widget and pass a reference to the Stock_Quote_Class to the widget to act as a callback.

This seems like a perfectly acceptable solution, and for simple plugins could well be good enough. But if your plugin is anything more than a few lines, then in the case above you run the risk of causing problems as you change the Stock_Quote_Class.

If you change a method on the Stock_Quote_Class for example, you will then need to check any other scripts which reference the class to see if it is being used, and if the change effects the code in the scripts which call the class.

Now I come from a .Net background and in that world we often use interfaces to help us loosely couple objects together.

But in WordPress there is a different way. Hooks and Filters.

If you have written a WordPress plugin then you have dealt with hooks and filters but did you know you can write your own?

Now if we go back to the code example we can do something like this.


Now what is happening here? Well in the Stock_Quote_Class we are defining a custom filter. One of our own to do as it is told.

We define our custom filter using add_filter, the first argument is the unique name of the filter(aka slug in WordPress terms) and the second argument is the function we are going to delegate to and from which we will return some data. There are two more arguments you can pass to add_filter, which is described in the codex here. You can also use the corresponding function apply_filters_ref_array if the function you are going to delegate to has more than one parameter.

In the Stock_Quote_Widget class we then make a call to apply_filters passing in the filter slug as the first argument and any parameters for the delegate function as the second parameter. In our example the function get_special_data() has no parameters so we are passing in null. If we did want to pass in a parameter the apply_filters function would look like this.


I’m sure you get the idea.

Now say we want to change the data the widget needs to get. We decide to create a new function called get_extra_special_data for some nefarious reason. Now if we had not used filters we would have to go through the whole plugin to find get_special_data and change it to get_extra_special_data.

But because we have used filters all we need to do is change the add_filter statement.

and the job is done. If you do not want to pass in data but want to execute a method instead then you can do the same thing with hooks. For example in our Stock_Quote_Class say you wanted to execute a do_something_special method which takes a text string to display on the front end. Then in the Stock_Quote_Class you would have something like this.

and in the Stock_Quote_Widget you would pass in the slug as the first parameter and the parameter for the do_something_special function as the second parameter in the do_action function.

So there you have it. If you have some tightly coupled classes try loosening them up a bit with filters and actions. And if you have liked this article and found it useful then please leave a comment.

2 thoughts on “WordPress, Custom Filters And Decoupling”

  1. Pingback: looky
    1. Do both a blog after all is simply a website you update regularly. So why not start a website with some information which doesn’t update regularly, and have part of the site as a blog.

Think you could have written this better? Then leave a reply and tell me what you think.