Skip to content

Instantly share code, notes, and snippets.

@bencagri
Last active September 2, 2015 09:05
Show Gist options
  • Save bencagri/a7713733b237fee83413 to your computer and use it in GitHub Desktop.
Save bencagri/a7713733b237fee83413 to your computer and use it in GitHub Desktop.
Php Simple Hook
<?php
class hooking{
private $hooks;
function __construct()
{
$this->hooks=array();
}
function add_action($where,$callback,$priority=50)
{
if(!isset($this->hooks[$where]))
{
$this->hooks[$where]=array();
}
$this->hooks[$where][$callback]=$priority;
}
function remove_action($where,$callback)
{
if(isset($this->hooks[$where][$callback]))
unset($this->hooks[$where][$callback]);
}
function execute($where,$args=array())
{
if(isset($this->hooks[$where])&&is_array($this->hooks[$where]))
{
arsort($this->hooks[$where]);
foreach($this->hooks[$where] as $callback=>$priority)
{
call_user_func_array($callback,$args);
}
}
}
};
$hooking_daemon=new hooking;
function add_action($where,$callback,$priority=50)
{
global $hooking_daemon;
if(isset($hooking_daemon))
$hooking_daemon->add_action($where,$callback,$priority);
}
function remove_action($where,$callback)
{
global $hooking_daemon;
if(isset($hooking_daemon))
$hooking_daemon->remove_action($where,$callback);
}
function execute_action($where,$args=array())
{
global $hooking_daemon;
if(isset($hooking_daemon))
$hooking_daemon->execute($where,$args);
}
?>
@bencagri
Copy link
Author

bencagri commented Sep 2, 2015

//Sample
First create your functions.

add_action("before_head_close","add_analytics_code");
function add_analytic_code()
{
echo "<script> var... </script>"; //The analytics code
}
?>

Then insert them with hook

<head>
<?php execute_action("after_head_open"); ?>
<title><?php echo $this->title; ?></title>
<?php execute_action("before_head_close"); ?>
</head>   
<body>```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment