July 17, 2009   -   WordPress   -   18 comments

Translate:

I simply love wordpress mainly because of the architectural design it follows to deliver the good. WordPress provide a hook or filter functionality to almost everything that appears on your blog. If you are a wordpress plugin developer you surely must have used one of them, specially add_filter() hook.

add_filter functionality provides plugin developer a way to apply custom hooks on various page sections. For instance add_filter('the_content', 'HelloWorld') will force the content to be passed through the function HelloWorld($content) before being displayed to your blog users.

In this blog post I will show you how can you achieve doing the same thing for your custom PHP frameworks. Otherwise you can also use this in your day to day projects. We will start with a simple directory structure, assuming this is all we have in our framework to start with:

Files/Folder structure of our framework
– addHooks
    – index.php
    – applyHooks.php
    – hooks
          – Subscribe.php
          – Signature.php

addHooks: Our framework root directory.
index.php: Homepage for our framework.
applyHooks.php: Responsible for including and implementing various hooks.
hooks: Our hooks directory. We will put all our hooks here.
Subscribe.php: A simple hook which will add a subscribe my feed link.
Signature.php: Another simple hook which adds my signature.

Source code
Download the complete source code from here.

index.php

  // define some content
  // in this demo i will show to how to apply hook to $content, similar to wordpress blog $content
  $content = "In this blog post I will show you how to add wordpress like add_filter hooks in your PHP framework.";

  // Include our hook management file
  require_once("applyHooks.php");
  

Subscribe.php

  function Subscribe($content) {
    $subscribe = '';
    $subscribe .= 'Subscribe to my RSS feed';
    $subscribe .= '

'; return $subscribe.$content; } add_filter('the_content', 'Subscribe');

Signature.php

  function Signature($content) {
    $signature = '

'; $signature .= 'Abhinav Singh
'; $signature .= 'Follow on twitter @imoracle
'; return $content.$signature; } add_filter('the_content', 'Signature');

applyHooks.php

  /* plugin directory */
  $hook_dir = "hooks/";

  /* get all plugin files */
  $hook_files = glob($hook_dir."*.php");

  /* array saving all our hooks on the_content */
  $hooks['the_content'] = array();

  /* add_filter functionality */
  function add_filter($on, $func) {
    global $hooks;
    array_push($hooks[$on], $func);
  }

  /* include plugin files */
  foreach($hook_files as $hook_file) {
    require_once($hook_file);
  }

  /* implement the hooks */
  foreach($hooks['the_content'] as $hook) {
    $content = call_user_func($hook, $content);
  }

  echo $content;
  

Final Result
When you try to visit your framework opening page on your browser, i.e. index.php. You will see your $content being filtered/modified and displayed as:
add_filter_output

How does applyHooks.php works
So what allows us adding such hooks in our framework. call_user_func is a PHP function which allows you to call a user defined function. Here is in short how applyHooks.php work:

  1. applyHooks.php adds a method called add_filter($on, $func) before including the hook files
  2. add_filter() essentially takes input as $on = the element on which you want to apply the hook and $func = the hook you want to apply $on element.
  3. If you see our Signature.php code, you will find add_filter('the_content', 'Signature');. Which essentially means, apply the filter called $func=’Signature’, $on=’the_content’
  4. The applyHooks.php file keep registering these hooks and save them in $hooks array
  5. Finally it implement all hook one by one by calling the call_user_func function (PS: You may also want to use call_user_func_array)

Extending applyHooks.php
In this blog post I have shown a demo, how you can create a add_filter type functionality. You may want to extend this to include hooks like:

  • remove_filter
  • add_shortcode

and others.

Hope this will help you making your web apps more expendable and manageable. :D