level-level / clarkson-hooks
Solution for 'Just in time' WordPress Hooks.
Installs: 84 048
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 0
Open Issues: 0
This package is auto-updated.
Last update: 2024-08-27 15:46:03 UTC
README
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.
Setup
1. Define clarkson-hooks as a dependency.
composer require level-level/clarkson-hooks
It will load automatically.
2. Point Composer towards a directory to find your Hooks
namespace.
"autoload": {
"psr-4":{
"Hooks\\": "app/Hooks"
}
}
3. Define a hook
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.
What happens in the background
- 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.
Tips
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.