|
@Arlen: As Keith S points out
Adam Brown's List of Hooks
is the defacto resource of hooks for WordPress. However, it'st perfect:
-
It doesn show the hooks in order of when they are called,
-
It doesn provide file name or line number where invoked,
-
It doesn provide a number of arguments passed,
-
It'st a complete list because some hooks can be called dynamically,
-
And it doesn show hooks from plugins.
So while Adam's list is a great resource especially for understanding when hooks were historically added it'st nearly as useful as if you were able to instrument the hooks on any given page on your own site.
I've been toying with this idea for a while so your question triggered me to write a
plugin
called "
Instrument Hooks for WordPress
." You can find the
full source below
the screen shot and can you can also
download it from gist
here
.
So here's a screenshot of what the instrumentation looks like:
You trigger the instrumentation by using the URL parameter
instrument=hooks
, i.e.:
http://example.com?instrument=hooks
And as promised, here's the source (or download it
here
.):
get_results("SELECT * FROM wp_hook_list ORDER BY first_call"); $html = array(); $html[] = '
|
First Call |
Hook Name |
Hook Type |
Arg Count |
Called By |
Line # |
File Name |
'; foreach($hooks as $hook) { $html[] = "
|
{$hook->
first_call} |
{$hook->
hook_name} |
{$hook->
hook_type} |
{$hook->
arg_count} |
{$hook->
called_by} |
{$hook->
line_num} |
{$hook->
file_name} |
"; } $html[] = '
'; echo implode("\n",$html); } add_action('all','record_hook_usage'); function record_hook_usage($hook){ global $wpdb; static $in_hook = false; static $first_call = 1; static $doc_root; $callstack = debug_backtrace(); if (!$in_hook) { $in_hook = true; if ($first_call==1) { $doc_root = $_SERVER['DOCUMENT_ROOT']; $results = $wpdb->
get_results("SHOW TABLE STATUS LIKE 'wp_hook_list'"); if (count($results)==1) { $wpdb->
query("TRUNCATE TABLE wp_hook_list"); } else { $wpdb->
query("CREATE TABLE wp_hook_list ( called_by varchar(96) NOT NULL, hook_name varchar(96) NOT NULL, hook_type varchar(15) NOT NULL, first_call int(11) NOT NULL, arg_count tinyint(4) NOT NULL, file_name varchar(128) NOT NULL, line_num smallint NOT NULL, PRIMARY KEY (first_call,hook_name))" ); } } $args = func_get_args(); $arg_count = count($args)-1; $hook_type = str_replace('do_','', str_replace('apply_filters','filter', str_replace('_ref_array','[]', $callstack[3]['function']))); $file_name = str_replace($doc_root,'',$callstack[3]['file']); $line_num = $callstack[3]['line']; $called_by = $callstack[4]['function']; $wpdb->
query("INSERT wp_hook_list (first_call,called_by,hook_name,hook_type,arg_count,file_name,line_num) VALUES ($first_call,'$called_by()','$hook','$hook_type',$arg_count,'$file_name',$line_num)"); $first_call++; $in_hook = false; } }
}
|