What is a hook in WordPress?
In simple words
WordPress core runs a long list of steps every time a page loads: load plugins, set up the theme, query posts, print the head, print content… At many of those steps it shouts a name like init or wp_head. A hook is you saying “when you shout that name, run my function too.” That’s how plugins and themes change WordPress without editing core files.
// "When WordPress reaches 'init', also run my function"
add_action('init', 'myplugin_register_book_type');
function myplugin_register_book_type() {
register_post_type('book', ['public' => true, 'label' => 'Books']);
}
Say it like this in the interview
“Hooks are WordPress’s event system. Core and plugins fire named hooks at specific points, and we attach callbacks to them with add_action or add_filter. They let us extend or change behaviour without modifying core files, so updates don’t overwrite our changes. There are two types: actions and filters.”
They will probably ask next
- “What’s the difference between an action and a filter?”
- “Name some hooks you use often.”
init,wp_enqueue_scripts,save_post,the_content,pre_get_posts,admin_menu.
Traps that make people fail
- Saying “a hook is a function”. The hook is the named point; your function is the callback.