concept-image / wp-hook-attributes
Declare WordPress actions and filters with PHP attributes (#[Action], #[Filter], #[WhenConfig]) on Acorn classes.
Package info
gitlab.com/concept-image-public/wp-hook-attributes
pkg:composer/concept-image/wp-hook-attributes
Requires
- php: >=8.3
- roots/acorn: ^6.1
Requires (Dev)
- brain/monkey: ^2.6
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
- pestphp/pest-plugin-arch: ^4.0|^5.0
- szepeviktor/phpstan-wordpress: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
WordPress actions and filters as PHP attributes
Put #[Action] and #[Filter] on the methods of Acorn classes, and switch them on or off with config. Shared by concept-image/wp-starter-theme, concept-image/wp-fortress and the theme code built on them.
Requirements
- PHP 8.3+
- Roots Acorn 6.1+ (for
config())
Installation
composer require concept-image/wp-hook-attributes
Usage
use ConceptImage\WpHookAttributes\Attributes\Action;
use ConceptImage\WpHookAttributes\Attributes\Filter;
use ConceptImage\WpHookAttributes\Attributes\WhenConfig;
#[WhenConfig('theme.shop.enabled', default: true)] // the whole class
final class ShopHooks
{
public function __construct(private readonly ShopService $shops) {}
#[Action('wp_ajax_shop_status')]
#[Action('wp_ajax_nopriv_shop_status')]
public function status(): void { … }
#[Filter('the_title', 20)]
#[WhenConfig('theme.shop.titles')] // only this method
public function title(string $title, int $postId): string { … }
}
Register the instance (resolved from the container, so dependencies are injected):
use ConceptImage\WpHookAttributes\Support\HookRegistrar;
HookRegistrar::register($this->app->make(ShopHooks::class));
#[Action(hook, priority = 10)]/#[Filter(hook, priority = 10)]are repeatable. The number of accepted arguments is the number of method parameters.#[WhenConfig(key, default = false, equals = true)]works on a class or a method: truthy by default,equals: falsefor falsy, any other value for strict equality.- Static and non-attributed methods are ignored.
- New hook kinds implement
Attributes\Hook(register(callable $callback, int $acceptedArgs)), and the registrar picks them up without changes.
Development
composer ci # Pint, Larastan (level max), Pest
Without PHP's dom/xml/mbstring extensions on the host:
docker run --rm -u "$(id -u):$(id -g)" -v "$PWD":/app -w /app composer:2 composer ci.
Changelog
Please refer to CHANGELOG for more information.
License
Please refer to LICENSE for more information.
Credits
- Banner icon: Isometric icons created by Magnific - Flaticon