Solution for 'Just in time' WordPress Hooks.
Clarkson-hooks combines the all
action and composer autoloading to only include the filters you are actually going to use.
composer require level-level/clarkson-hooks
It will load automatically.
"autoload": {
"psr-4":{
"Hooks\\": "app/Hooks"
}
}
Example minimal init.php (put this in app/Hooks/init.php
when using path specified in composer above.
<?php
namespace Hooks;
use Clarkson\Hooks\iHook;
class init implements iHook {
public static function register_hooks( $name ){
add_action('init', function(){
wp_die('Hello world');
});
}
}
Note: the \Clarkson\Hooks\iHook
interface makes sure you correctly define your Hook
object.
For a real live example, check out the init hook in Clarkson Theme.
- An
apply_filters
ordo_action
is called from WordPress. - Just before the actual hook is triggered, the
do_action('all')
is caught by Clarkson-hooks. - Clarkson-hooks checks for the existence of
\Hooks\{hook-tag}
(Composer handles loading any corresponding file). - The correspondig class gets the static method
register_hooks
called. The actualadd_filter
oradd_action
is done in this file. - WordPress continues as expected.
Be sure to use the --optimize-autoloader
composer flag on production to have the loading process moving smoothly. Otherwise the class_exists function creates a lot of overhead.
All hooks initialize only once.