1

If a theme and a plugin uses the same action hook, for example comment_form is used by both theme and plugin. Then , which one will be called first? the function associated with the theme's hook or the function associated with the plugin's hook.

4

An action hook is simply a queue point that acts at a specific point in the PHP execution process: callbacks are queued via add_action() call, and everything in the queue gets processed in turn:

add_action( 'hook_name', 'callback_name', $priority, $number_of_args );

A filter hook is a similar queue point, only it acts on a specific bit of data - it could be a string, an array, an integer, or whatever. As with actions, callbacks are queued via add_filter() call, and everything in the queue gets processed in turn.

add_filter( 'hook_name', 'callback_name', $priority, $number_of_args );

If you need to ensure that one callback gets processed earlier or later than another, then you will need to ensure that the two callbacks have different priorities. The lower the number, the higher the priority, and the earlier the execution. The default priority is 10, so anything added with a priority of 11 will execute after the default, and anything added with a priority of 9 will execute before the default.

So, yes: both the Theme and Plugins can add action or filter callbacks to the same action/filter hook, without conflict.

  • 2
    Just a short note: In the absence of priority arguments, plugin callbacks should run before theme callbacks since unprioritized callbacks are added first-come-first-serve and plugin files are parsed first. – s_ha_dum Oct 24 '13 at 14:42
  • Another short note: When the two hooks have callbacks with the same priority, it's the "first-in, first-out" rule (fifo) as far as I understand the order in $GLOBAL['wp_filter']. Maybe you can elaborate on that case? – birgire Oct 24 '13 at 15:06
  • When it comes to the_content, shortcodes or appending/prepending html, you're boned if you need to control priority. Priority 11 runs do_shortcode, you CANNOT add a filter with shortcodes BEFORE this in priority 11 and priority 10 runs autop, so you CANNOT add html in priority 9, or your content will print without paragraph tags. Let's say a plugin adds some HTML to the end of the_content, the only way you'll be able to add some HTML above that, is by creating a plugin with a name that is alphanumerically ordered higher than it, and executing filter from there. It's an awful design. – Sandwich Jun 22 '17 at 6:08
1

Every action/filter hooks have a priority parameter(the third parameter). The default value of which is 10, the lesser the value the earlier it is called

Like

add_action( $tag, $function_to_add, $priority, $accepted_args );

add_filter( $tag, $function_to_add, $priority, $accepted_args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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