Does it matter if a hook’s callback function comes before or after its add_action? My answer might surprise you. No, it does not matter to PHP or WordPress. Yes, it does matter for code readability. I’ll explain why in this article.
Testing an Action Has a Callback
Testing a Callback is Registered to an Action Hook
Brain Monkey’s Hook Toolset
The Problem – Why We Need to Simulate WordPress
Testing Hooks with Brain Monkey
Our themes and plugins use hooks to run code when an event fires. We use WordPress’ functions to add or remove a hooked callback, fire the event, or check the status. How do you unit test those interactions without loading WordPress? Meet Brain Monkey. Brain Monkey gives you the power to unit test hooks by simulating WordPress. In this lab, you’ll walk through building unit tests for hooks.
Testing a Action Fired
Beans Solves the “Removing a Hooked WordPress Action too Soon” Timing Issue
A common problem in WordPress is removing a hooked action (via remove_action) too soon, i.e. before the original action is added via add_action. The Beans framework solves this problem through its Actions API. It’s a major enhancement to the WordPress Plugin API. Let me show you.
Dive into Beans’ beans_open_markup()
Let’s dive into the Bean’s framework to understand how the beans_open_markup() function works.
Callbacks and Namespacing
In WordPress, we register callbacks to events by using add_action or add_filter. In the last episode, you learned that you must specify the fully qualified name to invoke a function (i.e. run it) outside of the namespace. This applies to add_action or add_filter. In this episode, you will learn why as well as a shorthand version using the magic constant __NAMESPACE__.