Share this question

Welcome to Teachnovice Q&A, where you can ask questions and receive answers from other members of the community.

This is a collaboratively edited question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Writing a Pluggable PHP Application – Part 2?

1 like 0 dislike

I take no credit for this. This was posted on failover by Jethro Solomon.

In the first part of the series, I explained the basic usage of hooks, namely action and filter hooks. I Demonstrated a simple method of implementing action hooks. We will now have a look at filter hooks.

Filter Hooks – What We Will Accomplish

We are going to provide a method for you or third party users to modify or parse variables used in your application by creating a custom function and letting the application ‘know’ about it.

Filter Hooks – The Base Code

The following code is very similar to that of the action hooks. This time, we have the array, $filter_hooks to store the user-registered events and functions.

$filter_events = array();

The function hook_filter() accepts two arguments, that, being the name of the event and the variable to be used. This function is used by you.

function hook_filter($event,$content) { 
    global $filter_events;
        foreach($filter_events[$event] as $func) {
            if(!function_exists($func)) {
                die('Unknown function: '.$func);
          $content = call_user_func($func,$content);
    return $content;

The function register_filter() also accepts two arguments; first, the name of the event and the second, the name of the function to be called (that will process or use the variable). Again, this function may be used by a plugin developer for your software.

function register_filter($event, $func)
    global $filter_events;
    $filter_events[$event][] = $func;

Filter Hooks – Putting them into action

Continuing with the CMS scenario, we now want to provide a way for people to manipulate variables before displaying, inserting them into a database or whatever else you desire. So, we have a simple function to display content. To allow users to manipulate the content, we declare our function and register the filter hook:

function show_content($content) {
    //the content is assigned the return value of the hook_filter() function,
    //which is the result of persing all custom functions with the variable.
    $content = hook_filter('the_content',$content);
    echo $content;

Our plugin developer now wants to replace all instances of ‘ice’ with ‘hot’ before the content is displayed. He registers the filter, assigning his custom function that will accept the $content as an argument and return the modified string. Note that he does not call his custom function, that is handled by the hook_filter() function:

function hotstuff($var) {
    $var = str_replace('ice','hot',$var);
    return $var;

Now, in your application, if you were to run the following code, the output would be “Hello. I like hot cream”

show_content('Hello. I like ice cream.');

Thanks again for reading, the final part of this series (no. 3) will show you how to put everything we have done together (in a neat directory structure), and use a simple method for loading plugins for your app.

I will also touch on how the Big Cats, such as WordPress, actually implement their hook functionality.

asked Oct 16, 2016 by pak786 (2,100 points)  
reopened Oct 24, 2016 by pak786

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.