WP AutoHooks is a PHP Library that allows you to define Attributes on Classes and Methods to document and register WordPress Hooks.
- Developer Experience: Document hooks next to the method, giving you context and a better developer experience.
- Performance: Hooks are registered without loading classes or instantiating objects until needed.
- Flexibility: The underlying PHP Library can be used for both simple and complex projects.
- Modularity: Ability to add and remove standalone classes quickly and easily.
By default, the library is geared toward Composer, but there is flexibility, and you can integrate the library with your class loading system.
composer require spenserhale/wp-autohook-library
Attribute your class and methods with the Hook
and Shortcode
attributes.
#[Hook('cli_init')]
public static function register(): void {...}
// Get the list of classes
$classes = \SH\AutoHook\ComposerJsonParser::getClasses($composerJsonPath);
// Process classes to string
[$output] = \SH\AutoHook\AttributeResolver::processClassesToString($classes);
// Write to file
(bool) $written = \SH\AutoHook\FileWriter::write($output, $outputPath);
Some projects may not have the composer.json available at runtime, so you can use the class loader object.
// Get Classloader object
$loader = require 'vendor/autoload.php';
// Get the list of classes
$classes = \SH\AutoHook\ComposerClassLoaderParser::getClasses($loader, ['App\\', 'MyNamespace\\']);
// Process classes to string
[$output] = \SH\AutoHook\AttributeResolver::processClassesToString($classes);
// Write to file
(bool) $written = \SH\AutoHook\FileWriter::write($output, $outputPath);
To run tests, make sure to create class list through composer:
composer du -o
Then run the tests:
composer test
The WordPress AutoHooks Library is open-sourced software licensed under the MIT license.