How does WordPress call functions attached to a certain action hook before calling functions attached to other hooks

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?

Solutions Collecting From Web of "How does WordPress call functions attached to a certain action hook before calling functions attached to other hooks"

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?

WordPress doesn’t execute all the action and filter 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 included PHP file & in what line those function calls are.

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.

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?

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.