We can use functions like register_nav_menu() directly in function.php and it works and we can also use a function to put register_nav_menu() inside of it and attach that function using a hook like init and it works as well.

Why do we need to use a hook in that particular situation?


Imagine 2 people are cooking food, and both are following a recipe, when both reach instruction 5. This instruction tells them what to do with some uncooked meat that may be dangerous to eat uncooked.

In Alices's cookbook she sees:

When the meat is safely cooked, eat the meat

In Bobs's cookbook he sees:

eat the meat

Alice cooks the meat and goes home with a full stomach.

Bob eats uncooked meat, and is rushed to A&E several hours later with E coli poisoning.

A similar case is true of hooks and filters. By using a hook to do work, you're specifying when that work happens. By putting it straight in the functions.php file, it will happen as soon as the file is loaded, and that isn't always appropriate. Perhaps something hasn't been loaded yet? Or something must happen last or as early as possible?

A week later, person C and D attempt to follow the first recipe, both are using a revised recipe with a 6th step. They heard what happened to Bob and decided to makes some modifications.

Person C sees:

6: when you have friends around, add expensive sauce

Person D sees:

6: add expensive sauce

Expensive sauce is very expensive, there are only 5 bottles of it! Neither person has friends, so Person C saves money by not using sauce. But Person D always adds expensive sauce, even when it isn't needed, because their recipe doesn't specify when it should and shouldn't be added.

This is also true of hooks. Sometimes you only want things to happen when certain events occur, etc For example, I might perform a check on the admin_init hook, this way the check does not occur on the frontend, only the dashboard admin

| improve this answer | |
  • Like the way you explained it +1 – Pieter Goosen Jun 22 '14 at 19:27
  • 3
    Person C and D are very lonely people, if they don't understand events, they may never have friends and eat that expensive sauce – Tom J Nowell Jun 22 '14 at 19:30

Machines read code like (most Western European) humans read text: From left to right and top to bottom. And hooks and filters are right in the middle of text. A short example:

do_action( 'plugins_loaded' );

Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit​​, morbo vel maleficia? De apocalypsi gorger omero undead survivor dictum mauris.

do_action( 'zombie_entry' );

Hi mindless mortuis soulless creaturas, imo evil stalking monstra adventus resi dentevil vultus comedat cerebella viventium. Qui animated corpse, cricket bat max brucks terribilem incessu zomby. The voodoo sacerdos flesh eater, suscitat mortuos comedere carnem virus. Zonbi tattered for solum oculi eorum defunctis go lum cerebro. Nescio brains an Undead zombies. Sicut malus putrid voodoo horror. Nigh tofth eliv ingdead.

Now you could attach a callback to zombie_entry to add additional content in the middle of above paragraphs. Example:

add_action( 'zombie_entry', 'add_zombies' );
function add_zombies()
    echo 'Roooaaar!';

The first hook available for plugins is plugins_loaded, the first for mu plugins is muplugins_loaded and the first for themes is after_setup_theme. That means that plugins, theme files, etc. are loaded before that hook or filter. And that means that if you are not attaching a callback to a filter or hook, then you end up with code executing directly when the file gets loaded. Problems that can arise from that is, that you maybe not have access to function (defined later) or that prerequisites haven't yet been setup.

| improve this answer | |
  • 1
    As with Tom, I like the way you've explained it. +1 – Pieter Goosen Jun 22 '14 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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