Wallogit.com
2017 © Pedro Peláez
Solution for 'Just in time' WordPress Hooks.
Solution for 'Just in time' WordPress Hooks., (*1)
Clarkson-hooks combines the all action and composer autoloading to only include the filters you are actually going to use., (*2)
composer require level-level/clarkson-hooks, (*3)
It will load automatically., (*4)
Hooks namespace."autoload": {
"psr-4":{
"Hooks\\": "app/Hooks"
}
}
Example minimal init.php (put this in app/Hooks/init.php when using path specified in composer above., (*5)
<?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., (*6)
For a real live example, check out the init hook in Clarkson Theme., (*7)
apply_filters or do_action is called from WordPress.do_action('all') is caught by Clarkson-hooks.\Hooks\{hook-tag} (Composer handles loading any corresponding file).register_hooks called. The actual add_filter or add_action is done in this file.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., (*8)
All hooks initialize only once., (*9)