This question already has an answer here:

I am new to wordpress development. I am familiar with joomla's ins-and-out working mechanish, now i want to contribute or say learn wordpress. I found that every thing in wordpress is done through plugin and its hooks and filters. But before diving into writing some cool stuffs, i want to know how hooks registered in plugin executes ? which design patterns is implemented in wordpress plugin environment ?

OK, lets take example.

add_action( 'wp_head', 'my_plg_function' );
function my_plg_function() {
  echo 'I am called';

In above code, plugin's function is registered in wp_head hook, right ? Now what exactly does add_action function does and when wp_head hook runs, how my plugins function called ? Again I am new to this, i am trying to learn and create cool stuffs in wordpress. Any helps will be appreciated. Thanks

marked as duplicate by fuxia Jan 30 '15 at 6:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


do_action is an event and there are many of them throughout your WordPress instance, in the core or your themes and plugins. (Hypothetical) So even when you put an add_action in your plugin file but the theme files are read first by order of execution your add_action function will still fire at that event or time of the do_action it is hooked to.

This is because even the theme files are held before they are executed by a WordPress Core hook called after_setup_theme To know the order of all of these check out the documentation here

Essentially this means that all code should be inserted at some point in this action hook hierarchy, also you can create your own. So when adding code in a file you should be asking "when" in time is this executing? Have the theme files finished loading by this point? Has WordPress itself loaded yet? There are hooks for those things so be sure to read through that documentation

  • Can I ask why this was down voted? As far as I know, to be worthy of such an insult I would have to meet the following criteria: Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. From my mobile device I did more research then the OP when asking the question so I don't get it. – Xtremefaith Jan 30 '15 at 16:44

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