2

We know that there is a certain order for the execution of callback functions attached to action hooks.

enter image description here

How does WordPress decide which action should be prioritized while executing all the hooks?

1
  • By the by Wordpress is not a framework. It's a Content Mangement System ( cms ). Commented Apr 2, 2017 at 18:48

4 Answers 4

2

WordPress doesn't execute all the actions and filters (i.e. hooks) at the same time using some sort of priority, instead, execution order of these action and filter hooks are hard coded within WordPress core PHP files.

Within WordPress source CODE, search for the following four function calls (using any text editor's find in files feature):

  1. apply_filters => applies a specific filter.

  2. apply_filters_ref_array => applies a specific filter, specifying arguments in an array.

  3. do_action => does a specific action.

  4. do_action_ref_array => does a specific action, specifying arguments in an array.

You'll see that each of the above function calls are used multiple times within the WordPress source CODE and execution order (priority) of the corresponding action and filter hook depends on when WordPress makes those function calls.

As PHP itself executes CODE line by line, execution order of those functions and their corresponding hooks also depend on which PHP file & in what line those function calls are.

Note: Although execution order of each specific action & filter hook is pre-determined within the CODE, execution order for multiple use of a single hook is determined by the $priority parameter.

WordPress hard codes these function calls to make sure the execution order of these action & filter hooks are predetermined. Otherwise developers would have real hard time coding themes & plugins.

Note: Since 4.7 WordPress implements all of the above four functions internally using WP_Hook class. So you may also take a look at that class to understand how all these are done internally.

1

The order is determined in, well, how WordPress is coded. If you're trying to change the action execution sequence, you'll have to edit the WordPress core files (and break a lot of things).

If you're trying to make sure certain things are executed before anything else, you can just change how you hook into WordPress' actions.

Keep in mind that certain WordPress functions (such as is_single(), is_home(), etc.) will only work after specific WordPress hooks have run.

Loads of more information is here: How to get WordPress' hook run sequence?

1

Please read the following, you will get your answers

1) Is there a flowchart for wordpress loading sequence?

2) Between functions.php, widgets and plugins, which is loaded first?

0

Whenever a callback is added to a hook (either action or filter), it gets added with a priority. If no priority is specified, then the default value of 10 is used.

//* Add action to init at priority 4
add_action( 'init', 'wpse_262184_init', 4 );

For each hook fired, the callbacks are run in increasing order or priority (lower numbers first). If two callbacks have the same priority, they are run in the order they are added to the hook.

1
  • I am not talking about the call back function's priority here, certain action tags are called before some other action tags, for example, plugins_loaded action tag is called before load_textdomain tag, so the question is how does wordpress decide which action tag to call first. I am talking about the execution order of action tags and not the execution order of callback functions hooked to a certain tag.
    – IAmDhar
    Commented Apr 2, 2017 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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