1

I'm planning to build a simple application which includes modular functionality and I'm planning to use similar technology as WordPress uses, which is hooks!

I did some digging and didn't find a proper answer or found some but it's too confusing, so can somebody explain to me how the hooks functionality works and how can we implement it with core PHP!

My plan is to create a home page and add modules (like WordPress plugins) to the home page, I did a basic WordPress plugin and came across some hooks like add_action, has_action, remove_action and so on, and some filters like add_filter, has_filter, remove_filter and so on

how can we implement these functions from scratch with core PHP?

if there is any reference code, blog or the simple code to implement a basic hello world application will work!

I'm just trying to learn new things, please help me to add this functionality to the home page and explain to me the process if you can! thanks.

rakcode
  • 2,256
  • 4
  • 19
  • 44
  • 1
    Possible duplicate of [PHP Event-Listener best-practice implementation](https://stackoverflow.com/questions/4471183/php-event-listener-best-practice-implementation) – bluemoon6790 Feb 14 '19 at 21:54
  • For reference code see WordPress Codex, it has examples and your questions shows that you should give it some time: https://codex.wordpress.org/Plugin_API/Action_Reference – Mulli Feb 14 '19 at 22:03
  • 1
    I would look at some apps to get an idea, but no, not Wordpress, we don't need anything else like that in the world. – AbraCadaver Feb 14 '19 at 22:10

2 Answers2

2

My best advice to you is to do some digging in the WordPress code and see how they did it.

That said, it is my understanding that WordPress uses php's $$ functionality. You can read about it here.

The jist of it is let's say I have a variable $a. I can reference it as $a and get it's value. However, let's say I don't know the value I want is in $a. But I do know that it is in the variable named by $b. That is to say, $b = "a". What $$ allows me to is say echo $$b and it will echo the value of $a.

Here's a more complete example:

$a = "foo";
$b = "a";

echo $$b;

Output:

foo

Neat, huh?

Finally, I should mention that Wordpress applies $$ to functions. It will be easier if I just make a code block:

$hooks = array();

function AddHook($hook,$funciton) {
    $hooks[$hook][] = $function;

}

function GetHook($hook) {
    foreach($hooks[$hook] as $func) {
         $func();
    }

}

Or something to that effect. So, you can use it like this:

function myFunc() { /* Code */}

AddHook("myHook","myFunc");

GetHook("myHook");

P.S. I guess that would make Wordpress a mediator design pattern.

0

The Chipster answer is correct and works, but you have to declare the $hooks variable in each function as global in order for the full code to work. Thanks anyway to Chipster for the clearer (if not the only) answer about how to create hooks in PHP.

<?php
global $hooks;

$hooks = array();

function addHook($hook, $function)
{
    global $hooks;
    $hooks[$hook][] = $function;
}


function getHook($hook)
{
    global $hooks;
    foreach ($hooks[$hook] as $func) {
        $func();
    }
}

function myFunc()
{
    echo "hello!";
}

addHook("myhook", "myFunc");

getHook('myhook');
Lucas Cave
  • 73
  • 8