Download the PHP package boxuk/wp-hook-attributes without Composer
On this page you can find all versions of the php package boxuk/wp-hook-attributes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download boxuk/wp-hook-attributes
More information about boxuk/wp-hook-attributes
Files in boxuk/wp-hook-attributes
Package wp-hook-attributes
Short Description A mu-plugin to allow the use of PHP attributes for WordPress hooks
License MIT
Informations about the package wp-hook-attributes
WordPress Hook Attributes
Installation
composer require boxuk/wp-hook-attributes
Enable caching (recommended for production when using annotations)
Basic array based caching is enabled as standard but in production you may wish to bring in a more optimal adapter. Below is an example using memcache, but any PSR-6 adapter is supported.
composer require cache/memcache-adapter
Note: This only applies when using annotations, not needed when using PHP8 and attributes.
Usage
Now you can annotate functions and methods with attributes to attach them to a hook.
Not on PHP8 yet? You can use annotations instead
Note: Anything lower than PHP 7.4 is not supported.
Registering a namespace or prefix (highly recommended)
You likely want to register a namespace or prefix to ensure it only looks for attributes/annotations for your code. You can do so via the following hook:
If you're using annotations and don't do this it will likely be extremely slow
It does a
stripos()
comparison, so you can just put the first part of the namespace/prefix.
Registering files and classes
Currently only works with defined functions and declared classes that are registered before the init
hook. To get around this you can register function files or classes manually using the following hooks. This will need to be done prior to init
though, or the resolver will need to be called manually (details below).
Ignoring existing annotation names
Sometimes you may get errors when using annotations that an existing annotation hasn't been imported. This is because sometimes you find non-standard annotations or docblock parameters that we need to ignore.
Some common WordPress and related libraries are ignored by default, but it won't cover everything.
You can ignore any custom annotations you need to with the following hook:
Limitations
Attributes on hooks prior to init
require a bit more work
If you wish to use hooks prior to the init
hook, for example muplugins_loaded
you will not be able to use attributes for these without a bit more effort. As they would have already been called by the point the hook resolver is called, you will need to call the hook resolver yourself manually. For example, let's say you have a hook on muplugins_loaded
which is a pre-init hook.
The muplugins_loaded
hook would have already been called by the time our init
hook is called which calls the hook resolver. So in these scenarios, you'll need to call the hook resolver manually, e.g.
See the next limitation as to why we don't load the resolver this early by default.
The main hooks that this applies to is (but not limited to):
Source: http://rachievee.com/the-wordpress-hooks-firing-sequence/
Functions/methods must be registered before the init
hook
Attributes should work for any function/method registered before the init
hook is called. Any function/method that is registered as part of an mu-plugin
or a theme
should work as the hooks to load these are called prior to init
.
What won't work is any function/method that is registered after the init
hook, for example the following won't work because wp_loadded
is called after init
and thus the functions within my-functions.php
won't be registered in time:
You can register files manually, but again, this must be done before init
, so to fix the above you can do:
Similarly, simply requiring it will work also:
Non-static methods are not supported
If you have a method which relies on an instance of the current object, for examples:
You are able to set up a callback using an instance of Example
, e.g.
However, this isn't supported with this library because it cannot make an assumption on how it instantiates the class. Therefore, only static methods will work. It also requires methods are marked as static even if they are implicitly static. This is good practice anyway as using a method as static if not explicitly declared will raise a PHP Deprecated on PHP 7.4 and a Fatal Error on PHP 8.
All versions of wp-hook-attributes with dependencies
cache/array-adapter Version ^1.1
doctrine/annotations Version ^1.13