This started out as a PHP regex experiment. I wanted to find the WordPress “action” hooks in a particular theme so that I can add my own action function to it.
So instead of searching through the whole lot of files for do_actions I sat down writing a regular expression to do this. I was very satisfied with the results which made me think about creating a tool to do this.
So here you go a standalone PHP script which seeps through the PHP code of the WordPress core, plugins, themes or anything you specify and gets you a list of actions and filters.
I use the word “standalone” because this isn’t a WordPress plugin or anything which requires WordPress functions. This is a script which uses a regex in preg_match_all() to find all “tag” names in the do_action() and apply_filters() function. This does not even require WordPress, if you want to find the hooks in a plugin just download the plugin, unzip it to a directory readable by this tool.
Download WordPress Hooks Search Tool | GitHub
Usage
Upload the code to your document root (or anywhere else) and access it from the browser.
http://<yourdomain>/wp-hooks-search.php
Enter the directory or file to search for hooks. It can be either relative or absolute. If a directory is entered all subdirectories are also scanned. If you wish to scan the current directory enter a dot (.) in the text box.
Note: On Windows (WAMP and XAMPP users) you must use forward slash in the path instead of the usual backslash like this
wordpress/wp-content/themes/customizr
The second textbox is optional. If you are searching for a particular name in the hook you can enter it there.
Screenshots
Regular Expression
Even if this tool has 250+ lines of code the hero of this script is the regular expression which hooks out the hooks 😀
/(apply_filters|do_action)\s*\(\s*[\'\"](.+?)\s*[,\)]/
A do_action or an apply_filters function can look like any of the following.
do_action('wp_my_action',$variable); do_action ( "wp_head" ); apply_filters('filter_name', $value); apply_filters('filter_' . $variable, $value);
Here is the anatomy of the regular expression.
(apply_filters|do_action) – Match the word “apply_filters” OR “do_action”
\s* – The \s matches a whitespace character and putting an asterisk (*) in front of it matches zero or more of whitespaces. This match has been placed in several places because the function could be anything like these.
do_action('wp_my_action',$variable); do_action( 'wp_my_action', $variable ); do_action ('wp_my_action',$variable );
\( – Matches the opening brackets of the function.
[\’\”] – Match single quotes OR double quotes. Functions may use single or double quotes around their parameters so this matches any.
(.+?) – This is the portion that matches the first parameter of the function i.e. the hook, .+ matches one or more characters while the question mark ? does a non greedy match. Read more about regex greediness here.
[,\)] – This matches a comma OR the closing brackets. The second argument is optional in the do_action() function is optional so a comma matches if there is a second parameter like this.
do_action( 'wp_my_action', $variable );
If there is only one parameter the brackets match.
do_action( 'wp_head' );
So the comment form is all yours send me your bouquets and brickbats on this script
Lee says
I like this script. Works well in my test environment. Not sure when I will use it but I do know I will and it will save me lots of time. Thank you.
Lee says
Same Lee as above. Just letting you know I’ve used this script many times since first download. Will write a post about the script soon and hopefully send you a few visitors.
You might or might not have thought about this, but a really neat feature would be to make it possible to view the function a filter/hook is found within. Maybe use a ‘magnifying glass’ icon next to the filter/hook then load a new tab/modal when the icon is clicked.
Glenn Kelley says
Amazing addition to our toolset. This allows us to find issues within themes and sites on our network – allow for us to understand better what a theme and/or application is doing and allows us to react to our customers requests for support. We are a hosting company – and their code is their responsibility but we try to help where we can …
I used this to chase down an issue with a Christian News Media Outlets theme.
I was able to drop their load times from over 54 seconds to now just 2.4 seconds
Amazing tool – Keep up the good work – I would have purchased it … 🙂
Anh Tran says
One small question: how can I update the script when a new version of WordPress is released? Or will it work with any version?
Thanks.
John Rom says
For the sake of completeness, you might want to add the rare but useful “
do_action_ref_array
” to the regex.