kerryrandolph / wp-hook-annotations
Use PHP docblock @annotations to register WordPress hooks, filters and shortcodes
Installs: 1 256
Dependents: 1
Suggesters: 0
Security: 0
Stars: 7
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/kerryrandolph/wp-hook-annotations
Requires
- php: >=7.2
- doctrine/annotations: ^1.6
- doctrine/cache: ^1.8
- php-di/php-di: ^6.0
- psr/container: ^1.0
Requires (Dev)
- ext-json: *
- monolog/monolog: ^1.24
- phpunit/phpunit: ^8.1
- psr/log: ^1.1
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: 3.4.1
This package is auto-updated.
Last update: 2025-10-06 03:56:22 UTC
README
Use PHP Docblock @annotations to register WordPress hooks, filters and shortcodes.
Requirements
- PHP 7.2+
- PHP-DI 6
Install
Via Composer
$ composer require kerryrandolph/wp-hook-annotations
Usage
Instead of wiring callbacks with boilerplate add_action(), add_filter(), or add_shortcode(),
simply add the annotations directly to the callback function's docblock:
/** * @Action(tag="wp_loaded",priority=10,accepted_args=1) */ public function doSomething(){ // do something }
The following annotations can be used:
/** * @Action(tag="the_hook_name", priority=1, accepted_args=1) * @Filter(tag="the_filter_name", priority=1, accepted_args=1) * @Shortcode(tag="the_shortcode_name") */
- The
priorityandaccepted_argsparameters are optional, and default to 10 and 1 respectively - Double quotes are required:
tag="double_quoted". single quotes will throw an exception
Wire multiple hooks to a single callback function:
/** * @Filter(tag="some_wp_filter") * @Action(tag="some_wp_action") * @Filter(tag="another_wp_filter") */ public function updateSomeValue(string $value): string { return 'updated'; }
Once you have added the hook annotations, you need to get the HookManager object to process them.
If you are using Dependency Injection, the easiest way is by using the provided HookAware trait:
class MyWordpressHookClass { use HookAware; /** * @Action(tag="wp_loaded") */ public function foo(){} }
The HookAware->processHooks method is triggered automatically by the DI container, and uses reflection to discover the hooks and wire them into Wordpress.
Alternatively, you could get the HookManager in the constructor via DI, and manually trigger processHooks:
__construct( HookManager $hook_manager ) { $hook_manager->processHooks( $this ); }
License
WP Hook Annotations is released under the MIT License.