Use the $wp_filter global variable to see the functions attached to an action. And see how *not* to use it!
WordPress is very flexible because it is so easy to extend it via actions and filters. Every page you see, whether in the front end or the admin area, is constructed by code being attached to various actions and filters. Tracking down the function involved with an element on the screen can be difficult but the $wp_filter global variable knows everything!
A client recently asked me to remove a “Read More” button from an area of their website. As the site uses the Genesis framework I immediately started searching the Genesis source for a string that was part of the button html (“more-link”). I love that the Genesis framework uses add_action() and remove_action() to determine what code to run – it gives a child theme or plugin a lot of control without modifying theme template files. This is also what I find challenging when developing with it – I am constantly searching the Genesis source to find the required code to include or remove.
Genesis Visual Hook Guide
The search for “more-link” didn’t turn up anything obvious so I installed the Genesis Visual Hook Guide plugin. I immediately saw that the code was was run during the ‘genesis_entry_content‘ action.
Here is what Genesis Visual Hook Guide looks like when viewing actions on a Genesis powered site:
For some reason I didn’t run the Query Monitor plugin to look for the functions attached to that action but instead added some debug code in a comment.
$wp_filter and how *not* to use it
I knew that the $wp_filter global variable would list the functions attached to each action. I added code to display the contents of the $wp_filter variable. This was not a good idea.
The page crashed because the variable lists *all* actions and filters and *all* the functions attached to them. This was a massive list. I did a wget on the page and it downloaded an 85MB file!
I quickly modified the code to list the functions attached to the ‘genesis_entry_content‘ action.
Much better. I quickly found that the function in question was part of my own code! No wonder I wasn’t finding an obvious candidate in the Genesis source code.
This is the type of output from the above code. Note that the priorities are the first elements of the array.
Query Monitor Would Have Worked
Query Monitor has a section listing hooks. After finding that ‘genesis_entry_content’ was involved I should have looked in that section on Query Monitor. Old habits (adding debug code in a comment) are hard to shake.
Leave a Reply