[ad_1] When I first started writing about anonymous functions in WordPress back in January, I didn’t anticipate it spanning over three articles reaching into the fourth month of the year. But here we are. That said, this final article in the series aims to help provide a short introduction to a number of technical ideas both in PHP and WordPress to explain why deregistering anonymous functions is nearly impossible. And it provides a way for us to actually get contextual information about every single hook and callback in an instance of WordPress so we can handle the anonymous functions as we see fit all through the use of a plugin. Recall from the first article, the reason I started writing about anonymous functions in WordPress all stemmed from a tweet (or post or whatever they are currently called) that stated: I do wish developers would stop using WordPress hooks with (what I think are called) anonymous functions […]. They are very hard if not impossible to unhook. And the short of it is that it’s true: Anonymous functions are easy to register against WordPress hooks and are difficult to deregister. But it’s not impossible. Deregister Anonymous Functions in WordPress This is not what it means to remove a function from a hook. If you’ve stumbled across this article, here’s this is what the first two articles covered (should you be interested in reading them): In the latter, I provided some examples for how to programmatically identify the anonymous functions that exist in the instance of WordPress in which said code is running. Further, I provided an example function to include in the code so you can easily trace what the aforementioned code was doing. A Brief Summary of Anonymous Functions in WordPress Though I obviously recommend reading those articles (1, 2), here is a brief summary of what’s covered between the two of them: Anonymous functions (also called closures in PHP) are useful when you have a small, one-time callback that doesn’t need to be referenced elsewhere in your code. When an anonymous function is registered with a WordPress hook, PHP generates a unique ID for the closure using the spl_object_hash() function, and associates this ID with the callback function. This allows WordPress to track and manage callbacks even when they are anonymous functions. I show how to get the closure ID generated by PHP for a specific anonymous function callback. I talk about the global $wp_filter object and explain how it holds information on all registered actions and filters in WordPress. I share information for how to easily list all functions/callbacks hooked to a specific hook like wp_enqueue_scripts. I provide an example for how to create your own anonymous function hooked to wp_enqueue_scripts for testing purposes. The debug_backtrace() PHP function is used to render information about the call stack, including the object representing the anonymous function. Through the debug_backtrace() function, I show how to obtain the internal ID generated by PHP for a given anonymous function that’s registered with a specific hook. And at the end of the second article, I wrote: In the next article, I’ll walk through a process – regardless of how manual it is – for how to deregister the functions. How To Identify Anonymous Functions in WordPress And that’s what I intend to do in this article. Further, I’ll share a utility that I’ve written that helps give context to each function – anonymous or not – and where it’s located within the instance of WordPress where it’s running A Brief Survey of Technical Details Though you can skip to the end of the article to see the practical aspect of locating anonymous functions and deregistering them, there are technical concepts worth reviewing that are helpful in understanding why dealing with anonymous functions are problematic and the strategies we can use to locate them. Is this how you picture anonymous functions? Closures and Anonymous Functions The terms ‘anonymous functions’ and ‘closures’ are often used interchangeably in WordPress. Though they are similar, there are also differences worth noting. Consider the definitions of each: A closure is a special type of anonymous function that has access to variables from the outer scope even after the outer function has returned. It’s like having a function within a function and the inner function has access to the variables in the function that contains it. An anonymous function is a general term for any function defined without a name. In PHP, anonymous functions can be used just like named functions, but they are typically used as callback functions or for short, one-off tasks. They are similar in the following ways: Both are defined without a name, hence the term “anonymous.” They are typically used when you need a small, one-time-use function without the need to give it a specific name. Both can access variables from the enclosing scope through variable capturing. This means they can “close over” variables and maintain their values even after the enclosing function has finished executing. While both closures and anonymous functions are anonymous and can capture variables from the surrounding scope, closures have additional capabilities related to variable scope and lifetime management; however, in common use, the term “closure” is often used to refer to any anonymous function, leading to the confusion between the two terms. So when discussing anonymous functions in the context of WordPress hooks, it’s absolutely more accurate to state ‘anonymous functions.’ In short, perhaps it’s easy to remember it by saying “all closures are anonymous functions but not all anonymous functions are closures.” Garbage Collection in PHP Collecting garbage computers is not the same as garbage collection in a computer. When variables or objects are created in PHP, memory is allocated to store their values; however, when these variables or objects are no longer needed, they occupy memory space unnecessarily. Because of that, these values are referred to as garbage. Periodically, PHP will run an internal mechanism looking for values in memory that are no longer needed and free
Continue reading