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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package wp-hook-attributes

WordPress Hook Attributes

Build Status

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

PHP Build Version
Package Version
Requires php Version ^7.4 || ^8.0 || ^8.1
cache/array-adapter Version ^1.1
doctrine/annotations Version ^1.13
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package boxuk/wp-hook-attributes contains the following files

Loading the files please wait ....