Identifying WordPress Hooks
In order to develop WordPress solutions, you are required to understand how WordPress hooks work. Sadly WordPress has over 1000 hooks in the system, so the task may be daunting to know which hook to exactly use.
There are a few options to find out which hook to use:
- Comb through the source code
- Comb through WordPress documentation on hooks
- Search the Internet for the right hook to use.
Each one requires you to do some searching. Wouldn't it be easier if we let WordPress do that job for us? Whenever we load a page, WordPress will tell us what hooks were called, and in what order.
do_action
Note: WordPress has two kinds of hooks. An action hook, and a filter hook. This article only deals with the action hook. But it can also apply to the filter hook. The filter hook can also be found in wp-includes/plugin.php on line 134
WordPress calls its hooks via the function do_action. do_action calls out all the hooks attached to the hook specified in its arguments. If you open up wp-includes/plugin.php, you should be able to find a function called do_action on line 299.
Note: I am currently running WordPress v3.0 beta. Your file name and line positions may be different.
In order to output the called hook, your first thought may be to enter echo $tag. Sadly this won't work as it will output text all over the WordPress application, which will make it difficult in identifying the hooks called.
FirePHP to the Rescue
Instead we will use FirePHP. Make sure you have FirePHP installed. You can also ensure FirePHP gets loaded on all PHP pages.
Now change the file to:
function do_action($tag, $arg = '') {
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
fb($tag);
Now reload the home page of your WordPress installation with FirePHP enabled and you should get a similar result.
- muplugins_loaded
- plugins_loaded
- sanitize_comment_cookies
- setup_theme
- load_textdomain
- after_setup_theme
- load_textdomain
- auth_cookie_malformed
- set_current_user
- init
- widgets_init
- wp_loaded
- posts_selection
- template_redirect
- get_header
- wp_head
- wp_enqueue_scripts
- wp_print_styles
- wp_print_scripts
- posts_selection
- posts_selection
- get_generic_template_loop
- get_sidebar
- get_search_form
- posts_selection
- wp_meta
- get_footer
- get_sidebar
- wp_footer
- wp_print_footer_scripts
- shutdown
Why don't you visit some other pages to test it out? It works in the Admin side as well.
Backtrace
This is all cool and all, but to get a better understanding of how the WordPress platform works, I sometimes I want to know where the hook was called.
Don't worry, we can solve that for you.
Replace the code you entered with:
function do_action($tag, $arg = '') {
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
$backtrace = array_shift(debug_backtrace());
fb(substr($backtrace['file'], strlen(ABSPATH)) . ':' . $backtrace['line'] . ' ' . $tag);
The result should be like so:
- wp-settings.php:151 muplugins_loaded
- wp-settings.php:193 plugins_loaded
- wp-settings.php:201 sanitize_comment_cookies
- wp-settings.php:239 setup_theme
- wp-includes/l10n.php:300 load_textdomain
- wp-settings.php:270 after_setup_theme
- wp-includes/l10n.php:300 load_textdomain
- wp-includes/pluggable.php:548 auth_cookie_malformed
- wp-includes/pluggable.php:55 set_current_user
- wp-settings.php:287 init
- wp-includes/default-widgets.php:1144 widgets_init
- wp-settings.php:296 wp_loaded
- wp-includes/query.php:2322 posts_selection
- wp-includes/template-loader.php:7 template_redirect
- wp-includes/general-template.php:26 get_header
- wp-includes/general-template.php:1554 wp_head
- wp-includes/script-loader.php:700 wp_enqueue_scripts
- wp-includes/functions.wp-styles.php:21 wp_print_styles
- wp-includes/script-loader.php:672 wp_print_scripts
- wp-includes/query.php:2322 posts_selection
- wp-includes/query.php:2322 posts_selection
- wp-includes/general-template.php:114 get_generic_template_loop
- wp-includes/general-template.php:84 get_sidebar
- wp-includes/general-template.php:146 get_search_form
- wp-includes/query.php:2322 posts_selection
- wp-includes/general-template.php:350 wp_meta
- wp-includes/general-template.php:55 get_footer
- wp-includes/general-template.php:84 get_sidebar
- wp-includes/general-template.php:1564 wp_footer
- wp-includes/script-loader.php:620 wp_print_footer_scripts
- wp-includes/load.php:517 shutdown
Now how cool is that?
Speedtest for TMnet Streamyx Solaris, Mont’Kiara
Location: Solaris, Mont'Kiara
Download: 1.75 Mb/s
Upload: 0.41 Mb/s
Ping: 53ms
Time: 9:11 AM MYT 9th March 2010
ISP: Telekom Malaysia Berhad
Rating: 2.1/5 stars
Server: Seri Kembangan
Distance: < 50mi
Location: Solaris, Mont'Kiara
Line Quality: B
MOS: 4.36
Ping: 37ms
Jitter: 26ms
Packet Loss: 0%
Time: 9:23 AM MYT 9th March 2010
ISP: Telekom Malaysia Berhad
Rating: 2.1/5 stars
Server: Kuala Lumpur
Distance: < 50mi
Note: This is a TMnet Streamyx 2 Mb/s Business package.
25 Random Things About Me
This was taken from a Facebook meme. This is a reproduction of my note.
I am reproducing here, because it reminds me of who I used to be, and who I am.




