In what sequence are the hooks fired when a post is "published"?

All we need is an easy explanation of the problem, so here it is.

I would be grateful if someone could point out a link. I understand this may have a very lengthy answer but I searched and couldn’t find any resource.

Alternatively, it would also be nice if someone can just state the order in which hooks are fired. I’m sure there wont be many since I am only concerned about the “published” posts.

Thanks in advance.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

What exactly do you mean with “published”?

One thing you can try is this :

http://planetozh.com/blog/my-projects/wordpress-hooks-filter-flow/

The script runs in your WordPress root (or in /wp-admin/ if you prefer) and enumerates filters that are loaded in your blog. Hooks are displayed alphabetically, and for each hook, active filters (or actions, they’re the same) are listed in their execution order, which is either defined by their priority or by their loading order. The purpose of such a list is to help hunt plugin bugs down, and let you wisely fine-tune plugin priorities if needed.

Edit : I just found one promising looking plugin for this task : http://wordpress.org/extend/plugins/wordpress-hook-sniffer/

Alternativly you can change the /wp-includes/plugin.php file and log all hooks fired into log files. You then just have to “publish” a post and look which hooks has fired.

Following changes you have to made:

// in apply_filters(..)
$log = array();
do {
    foreach( (array) current($wp_filter[$tag]) as $the_ )
        if ( !is_null($the_['function']) ){
            $args[1] = $value;
            $log[] = $the_;
            $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
        }
} while ( next($wp_filter[$tag]) !== false );
$filename = ABSPATH."/hooks.$tag.log"
file_put_contents($filename,file_get_contents($filename) ."\n". print_r($log,true));

// in apply_filters_ref_array(..)
$log = array();
do {
    foreach( (array) current($wp_filter[$tag]) as $the_ )
        $log[] = $the_;
        if ( !is_null($the_['function']) )
            $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));

} while ( next($wp_filter[$tag]) !== false );
$filename = ABSPATH."/hooks.$tag.log"
file_put_contents($filename,file_get_contents($filename) ."\n". print_r($log,true));


// in do_action(..)
$log = array();
do {
    foreach ( (array) current($wp_filter[$tag]) as $the_ )
        $log[] = $the_;
        if ( !is_null($the_['function']) )
            call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));

} while ( next($wp_filter[$tag]) !== false );
$filename = ABSPATH."/hooks.$tag.log"
file_put_contents($filename,file_get_contents($filename) ."\n". print_r($log,true));

// in do_action_ref_array(..)
$log = array();
do {
    foreach( (array) current($wp_filter[$tag]) as $the_ )
        $log[] = $the_;
        if ( !is_null($the_['function']) )
            call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));

} while ( next($wp_filter[$tag]) !== false );
$filename = ABSPATH."/hooks.$tag.log"
file_put_contents($filename,file_get_contents($filename) ."\n". print_r($log,true));

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply