We know that there is a certain order for the execution of callback functions attached to action hooks.
How does WordPress decide which action should be prioritized while executing all the hooks?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
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):
apply_filters => applies a specific filter.
apply_filters_ref_array => applies a specific filter, specifying arguments in an array.
do_action => does a specific action.
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.
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_home(), etc.) will only work after specific WordPress hooks have run.
Loads of more information is here: How to get WordPress' hook run sequence?
Please read the following, you will get your answers
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.