Search by

concept-image / wp-hook-attributes

concept-image

Declare WordPress actions and filters with PHP attributes (#[Action], #[Filter], #[WhenConfig]) on Acorn classes.

Package info

gitlab.com/concept-image-public/wp-hook-attributes

Issues

pkg:composer/concept-image/wp-hook-attributes

Statistics

Installs: 81

Dependents: 12

Suggesters: 1

Stars: 0

0.1.0 2026-11-25 16:13 UTC

This package is auto-updated.

Last update: 2026-11-26 08:03:47 UTC


README

WP Hook Attributes

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: false for 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