Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I follow Just-In-Time approach in hooking up my function to an action? I mean everybody just attach the functions to a very early stage like init which leaves the scope of optimisation. I saw in a Wordcamp presentation once that JIT will yield some performance and I totally agree with it but how do I find out what's the last hook I can hook to?

Anything which can help in understanding the WP code execution flow too?

share|improve this question
    
Is this curiosity, or do you have a specific problem you are trying to solve? I ask because you make a vague statement about performance. In my experience, trying to "improve performance" just on general grounds tends to be a waste of time. –  Peter Rowell Feb 28 '11 at 20:27
    
Learning it once will enable me to produce better code. Gain might not be always worth it but with time, I am sure it won't take more than a few seconds more to figure out whats the best hook to attach a function. –  Ashfame Feb 28 '11 at 20:49

3 Answers 3

Take a look at Mike's answer to a similar question more specifically the plugin he posted there can be used to create a list of all action hooks and filters that were called to generate that page in order of execution.

Hope this helps

share|improve this answer

A lot of the time, people use the init hook. But this can be a mistake for many cases of conditional execution. For example, at init time, the view type ('home', 'single', 'page', 'archive', etc) is not yet known. If you need to conditionally call your function based on is_page() or similar, you need to use a hook that executes later, like template_redirect.

Similarly, for admin-side events, it is usually better to use the admin_menu hook rather than admin_init.

The resources in the Q/A pointed to by @Bainternet are very useful, so you should definitely look at those. But I thought I'd just point out a couple of specific things that might be useful to know.

share|improve this answer

I myself use something different, but got this snippet to advance my hook "spoofer" one day.

function list_hooks( $filter = false ){
    global $wp_filter;

    $hooks = $wp_filter;
    ksort( $hooks );

    foreach( $hooks as $tag => $hook )
        if ( false === $filter || false !== strpos( $tag, $filter ) )
            dump_hook($tag, $hook);
}

function list_live_hooks( $hook = false ) {
    if ( false === $hook )
        $hook = 'all';

    add_action( $hook, 'list_hook_details', -1 );
}

function list_hook_details( $input = NULL ) {
    global $wp_filter;

    $tag = current_filter();
    if( isset( $wp_filter[$tag] ) )
        dump_hook( $tag, $wp_filter[$tag] );

    return $input;
}

function dump_hook( $tag, $hook ) {
    ksort($hook);

    echo "<pre>&gt;&gt;&gt;&gt;&gt;\t<strong>$tag</strong><br />";

    foreach( $hook as $priority => $functions ) {

    echo $priority;

    foreach( $functions as $function )
        if( $function['function'] != 'list_hook_details' ) {

        echo "\t";

        if( is_string( $function['function'] ) )
            echo $function['function'];

        elseif( is_string( $function['function'][0] ) )
             echo $function['function'][0] . ' -> ' . $function['function'][1];

        elseif( is_object( $function['function'][0] ) )
            echo "(object) " . get_class( $function['function'][0] ) . ' -> ' . $function['function'][1];

        else
            print_r($function);

        echo ' (' . $function['accepted_args'] . ') <br />';
        }
    }

    echo '</pre>';
}

If someone wrote this, pls name yourself as the author (with a link to orig. source). I forgot to safe the link.


Edit: You should also take a look into this page @Codex. There you can see the order in which order get triggered.
Should give you a start: /root/index.php > wp-blog-header.php > wp-load.php > wp-config.php > wp-settings.php > \wp-includes\load.php > \wp-includes\default-constants.php > ... best would be you grab some debug class from somewhere and just check the includes local.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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