If anyone is familiar with Lynda’s tutorials, I’ve recently reviewed WP and Genesis: Building Child Themes from Scratch:Remove Genesis Defaults.
The speaker mentions setting the priority of an overriding setting to a certain number, saying that the lower the number, the lower the priority.
I wanted to know how can one figure out the scale of the priority in the scheme of the file, how to set parameters of priorities if necessary, and are there any resources online that can tell me more about it (beyond what I’m asking)?
Thanks!
The priority comes into play when you are writing a custom function or filter that hooks into a
do_action
orapply_filters
that someone has coded – these exist both in the WordPress core code, as well as in plugins, themes, etc.If you are writing code that is going to “do” an action, you would write something like this:
add_action('action-hook', 'my_function_name', 10);
Where 10 is the priority. (Note: if you omit the priority – it is optional – 10 is the default value).
Now, sometimes you want your action (or filter) to be done after every other action that may have run. If that’s the case, then you’d set the priority to a very high number, so it will run last:
add_action('action-hook', 'my_function_name', 9999);
(Note that there’s no guarantee some other code doesn’t have a higher priority, so it’s a bit of a calculated gamble).
Or, if you read one of the links I provided in my comments – http://thereforei.am/2011/04/26/priority-1000-for-wordpress-hooks-actions-and-filters/ – you can take it the other way. If you want to be sure your action or filter runs before anyone else’s, you set the priority to a very low number:
add_action('action-hook', 'my_function_name', -1000);
Before you blow me up for a negative number here, it’s legitimate – read the article linked above.
These same things apply to filters:
add_filter('the_content', 'my_function_name'); // defaults to priority 10
add_filter('the_content', 'my_function_name', -1000); // runs early / before others
add_filter('the_content', 'my_function_name', 9999); // runs late / after other
So that means the lower the number the higher the priority. Priorities in WordPress are arbitrary. That means you set them relative to the function you are trying to override.
The best way to circumvent this arbitration is to do a search for the hook you want to latch on to. Using your favorite IDE, do a project wise search for the hook in question. Once the search is complete, pull up each hook you get back and inspect its priority.