Finding actual functions added to hooks and filters

This is more of PHP programming question, rather than WordPress one. 🙂

If you are interested in exploring function of WordPress core alone, there is number of ways dedicated to that:

  • Codex has function reference that is human maintained – so it’s not comprehensive but can include additional information for important functions;
  • xref is code reference, built by code from information in WP source;
  • there are third party code references, such as QueryPosts (disclosure – i made it 🙂

But if (when) you start to get interested in code in general (including themes and plugins) you will need more generic tools as well:

  • programming editors/IDEs are very apt at searching code;
  • there are command line search tools, such as ack;
  • same tools like xref can be used to generate index for code you are interested in.

Also since you are interested in something very specific right now (file and line for the function) here is snippet I came up with (it’s very basic and won’t show you things like hooked classes and object methods but will do for starters):

function show_hook_function_locations( $tag ) {

    global $wp_filter;

    if ( empty( $wp_filter[$tag] ) )
        return;

    foreach ( $wp_filter[$tag] as $priority => $functions ) {
        foreach ( $functions as $function ) {

            if ( is_string( $function['function'] ) ) {

                $reflect = new ReflectionFunction( $function['function'] );
                echo 'Function ' . $function['function'] . ' is in file ' . $reflect->getFileName() . ' on line ' . $reflect->getStartLine() . '<br />';
            }
        }
    }
}


// Example use
show_hook_function_locations( 'the_content' );

Example output:

Function capital_P_dangit is in file C:\server\www\dev\wp-includes\formatting.php on line 3263

Function do_shortcode is in file C:\server\www\dev\wp-includes\shortcodes.php on line 144

Function wptexturize is in file C:\server\www\dev\wp-includes\formatting.php on line 29

Function convert_smilies is in file C:\server\www\dev\wp-includes\formatting.php on line 1756

Function convert_chars is in file C:\server\www\dev\wp-includes\formatting.php on line 1077

Function wpautop is in file C:\server\www\dev\wp-includes\formatting.php on line 188

Function shortcode_unautop is in file C:\server\www\dev\wp-includes\formatting.php on line 282

Function prepend_attachment is in file C:\server\www\dev\wp-includes\post-template.php on line 1195