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.
Upload the code to your document root (or anywhere else) and access it from the browser.
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
The second textbox is optional. If you are searching for a particular name in the hook you can enter it there.
Even if this tool has 250+ lines of code the hero of this script is the regular expression which hooks out the hooks 😀
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