Download the PHP package syntatis/wp-hook without Composer
On this page you can find all versions of the php package syntatis/wp-hook. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download syntatis/wp-hook
More information about syntatis/wp-hook
Files in syntatis/wp-hook
Package wp-hook
Short Description WordPress hook with object-oriented programming
License GPL-2.0-or-later
Homepage https://github.com/syntatis/wp-hook
Informations about the package wp-hook
🪝 wp-hook
[!CAUTION] This package is now part of Codex.
A class wrapper designed to use WordPress hooks with object-oriented programming (OOP), inspired by the WordPress Plugin Boilerplate. This class helps you stay organized and maintain structure when managing hooks in your WordPress plugin or theme.
Installation
Install the package via Composer:
Usage
Create a new instance of the Registry class and register your hooks:
Using PHP Attributes
If your theme or plugin runs on PHP 8.0 or later, you can leverage PHP Attributes to define hooks directly within your classes:
[!NOTE] Attributes will only be applied to non-abstract public methods that are not PHP native methods or any methods that begin with
__like__constructor,__clone, and__callStatic. If you add the Attributes at the class level, the class should implement the__invokemethod, as shown in the above example.
Removing Hook
You can also remove a hook similarly to how you would with the native WordPress functions:
It is also possible to remove all hooks at once:
It is possible to attach hook with method from an object instance, a static method, or a closure:
However, this makes it rather tricky to remove the hook later on the code since you need to pass the same object instance or the same reference to the anonymous function to the removeAction and removeFilter methods, which is not always possible.
To circumvent this, you can pass id to the addAction and addFilter methods, and refer the id using @ symbol when removing the hook. For example:
[!IMPORTANT] The ID must be all lowercase and use words separated by
-,., or_. It should not have any uppercase letters, spaces, or special characters. You can use a slash (/) to define the namespace, likeacme/hello-world, to avoid conflicts with other plugins or themes. Please note that the ID added within the registry must be unique. If you're trying to add the same ID twice, it will throw an exception.