Creating a WordPress-Alike Hook For Your Own CMS

Hooks ilustration, image courtesy of lookingglassoutfitters.com

Probably you have known this, WordPress code is pretty good, especially when compared with other CMS or what we ever found on PHP tutorials. The one I like is how WordPress uses hooks.   If you ever created WordPress plugins, you may experienced that you don’t need to modify any of core code just to add something in the header and footer. Or even for more complex task, add additional menu and page on WordPress admin.

Things you need to do is to know what hook related to that, and add actions or filter with your own callback function. WordPress hooks divided in to two sections; Action hook and Filter hook. What is the difference? In simple, actions is to add functionality or output, where filter is to manipulate existing output. WordPress hook code stored in wp-includes/plugins.php

Err, I am not going to deeply explain about WordPress hook. But, in case you’re wondering how it works or may be you want implement it in your own CMS or PHP script, here is the step by step .

If WordPress uses global variable $wp_filter to store all of the filters, we use $cms_filter instead, or name it whatever you like .  This variable contains array of filters where we could modify later. To start, fill it with empty array.

1
$cms_filter = Array();
$cms_filter = Array();

Declare do_action() function, the task of this function is to call custom functions stored in $cms_filter, uses  call_user_func_array();

1
2
3
4
5
6
function do_action($tag){
global $cms_filter;
if(isset($cms_filter[$tag]) AND function_exists($cms_filter[$tag])){
call_user_func_array($cms_filter[$tag], array());
}
}
function do_action($tag){
global $cms_filter;
if(isset($cms_filter[$tag]) AND function_exists($cms_filter[$tag])){
call_user_func_array($cms_filter[$tag], array());
}
}

Declare add_action() function, the task of this function is to store custom function to $cms_filter global variable.

1
2
3
4
function add_action($tag,$callback){
global $cms_filter;
$cms_filter[$tag] = $callback;
}
function add_action($tag,$callback){
global $cms_filter;
$cms_filter[$tag] = $callback;
}

And………. you’re done. What..? Yes, that code is already to use.

Ok, let’s go. Place this code in wherever you like, for example in your core CMS file, or in your header’s template file between <head> and </head>

1
do_action('cms_head');
do_action('cms_head');

Whenever you want to add something to the location, just call this function:

1
add_action('cms_head','script_in_my_head');
add_action('cms_head','script_in_my_head');

cms_head is hook name, and script_in_my_head is must exists callback function that refer to your custom code. See example below:

1
2
3
function script_in_my_head(){
echo '<script>alert('Hello world!')</script>';
}
function script_in_my_head(){
echo '<script>alert('Hello world!')</script>';
}

Note: This is only a simple working code, not a replacement nor has same capability as actual WordPress hooks. You may edit or improve it to meet your need.

$10.99 .CO Domains!

Incoming search terms:

  • how to create your own wordpress cms
  • how to build a wordpress like hook
  • build your own cms in php
  • make your own cms
  • own cms php
  • php build own cms like wordpress
  • php cms how to create hook system
  • php own cms with hooks
  • startfill com
  • tutorial create hook with php
  • http://twitter.com/herihehe Heri Setiawan

    Dude, this is awesome. !

    • http://www.facebook.com/profile.php?id=1245322728 Ian Hehe Lubis

      Great, this tutorial very helpful

  • Nilesh Parate

    thanks for nice and functional