0

I'm trying to figure out how to find the actual function that's assigned to hooks and filters.

I've been looking at other questions asked here and found a couple plugins that are helpful for findings the add_actions and add_filters that show me the callback function name... but I'm having a hard time finding that actual function(s) so I can see what it does. They aren't always in the same file as the add_action or add_filter.

I just want to do this so I can wrap my head around the concept of hooks and filters better.

So for example, here's an add_action in shortcodes.php on line 335:

add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()

Here's the actual function on line 144, I want to be able to find these easily:

function do_shortcode($content) {
    global $shortcode_tags;

    if (empty($shortcode_tags) || !is_array($shortcode_tags))
        return $content;

    $pattern = get_shortcode_regex();
    return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
}

Any tips?

1
  • My advice, use a text editor that will let you do searches on a whole directory of files, recursively.
    – Otto
    Dec 23, 2012 at 21:30

3 Answers 3

1

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

1
  • Thanks! I didn't even realize that I could search all the files with my basic text edtior (Notepad++) ... I completely overlooked that capability... starting to find all the functions I'm looking for.
    – Josh
    Dec 28, 2012 at 16:16
0

There are several things you can do.

Google or the Codex will get you a long way. Adam R. Brown's database is useful.

But those are not usually my preferred mechanism.

The following will search files recursively downward from whatever directory your run it from.

grep -Rni "function callback_function_name" *

That is how I do it. grep is a *nix application that can be found or installed on just about any BSD/Linux box, including Macs if you open a terminal. There are Windows clones/near-versions as well, but I've never used them.

0

To see the callbacks registered for a specific hook inspect $GLOBALS['wp_filter']. I have written a small plugin for this: T5 Debug Hook.

After installation you get a very informative list of callback handlers for a hook if you call the page with ?hook=NAME.

Example example.com/?hook=the_content:

array (
  8 => 
  array (
    '000000001b2c775b000000006f220833run_shortcode' => 
    array (
      'function' => 
      array (
        0 => 
        WP_Embed::__set_state(array(
           'handlers' => 
          array (
            10 => 
            array (
              'googlevideo' => 
              array (
                'regex' => '#http://video\\.google\\.([A-Za-z.]{2,5})/videoplay\\?docid=([\\d-]+)(.*?)#i',
                'callback' => 'wp_embed_handler_googlevideo',
              ),
            ),
          ),
           'post_ID' => NULL,
           'usecache' => true,
           'linkifunknown' => true,
        )),
        1 => 'run_shortcode',
      ),
      'accepted_args' => 1,
    ),
    '000000001b2c775b000000006f220833autoembed' => 
    array (
      'function' => 
      array (
        0 => 
        WP_Embed::__set_state(array(
           'handlers' => 
          array (
            10 => 
            array (
              'googlevideo' => 
              array (
                'regex' => '#http://video\\.google\\.([A-Za-z.]{2,5})/videoplay\\?docid=([\\d-]+)(.*?)#i',
                'callback' => 'wp_embed_handler_googlevideo',
              ),
            ),
          ),
           'post_ID' => NULL,
           'usecache' => true,
           'linkifunknown' => true,
        )),
        1 => 'autoembed',
      ),
      'accepted_args' => 1,
    ),
  ),
  10 => 
  array (
    'wptexturize' => 
    array (
      'function' => 'wptexturize',
      'accepted_args' => 1,
    ),
    'convert_smilies' => 
    array (
      'function' => 'convert_smilies',
      'accepted_args' => 1,
    ),
    'convert_chars' => 
    array (
      'function' => 'convert_chars',
      'accepted_args' => 1,
    ),
    'wpautop' => 
    array (
      'function' => 'wpautop',
      'accepted_args' => 1,
    ),
    'shortcode_unautop' => 
    array (
      'function' => 'shortcode_unautop',
      'accepted_args' => 1,
    ),
    'prepend_attachment' => 
    array (
      'function' => 'prepend_attachment',
      'accepted_args' => 1,
    ),
  ),
  11 => 
  array (
    'capital_P_dangit' => 
    array (
      'function' => 'capital_P_dangit',
      'accepted_args' => 1,
    ),
    'do_shortcode' => 
    array (
      'function' => 'do_shortcode',
      'accepted_args' => 1,
    ),
  ),
)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.