Last Updated: February 25, 2016
·
1.596K
· leoj3n

DYNAMICALLY GENERATED hooks?

The following is something I didn't know about until I began digging into WordPress. It is shall be my first pro tip:

WordPress dynamically generates some of its action hooks. Such hooks are referenced in the action reference section of the codex but it's easy to miss their existence (at least I did, at first).

Go to

http://goo.gl/vqgXq

And do a Ctrl+F/Cmd+F for "-("

The bit inside the parens is what gets dynamically generated.

The stock example given in the codex uses the "adminhead-(pluginpage)" hook: http://goo.gl/EeEsP

Important note: For this hook and some others the codex says "or" in the following fashion: adminhead-(pagehook) or adminhead-(pluginpage). This is because page_hook was deprecated.

Such dynamic hooks are useful because they let you bind actions that run only on certain pages. However, only a subset of hooks are generated this way; there is no adminnotices-(pluginpage), only admin_notices.

In such instances just wrap the code in an if statement and test against the $pagenow global variable or the newer (since 3.0) getcurrentscreen() [http://goo.gl/VxeIp].