Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When I wish my filter or action hook to override all others, I will assign it a priority of 999. However, lately I have been seeing some people use extreme values for the priority, such as 20000, and even 99999

Besides the fact that using priorities this high is ridiculous, will they actually work? Is there a limit to hook priority? What will happen if the limit is exceeded? Is there a performance difference when using extreme priorities?

Update: @harke suggests on Stack Overflow that the number is limited by PHP_MAX_INT

share|improve this question
    
Didn't you link to an Answer by @hakre that talked about this? It should be part of the Q, and more, follow the lead that he gave, I suspect he seriously knows one or two things... –  brasofilo Jan 9 '13 at 11:44
    
To what answer are you referring? –  bungeshea Jan 9 '13 at 11:49
    
add comment

4 Answers

up vote 7 down vote accepted

There are no limits and no performance penalties. To understand why, you need to understand how all hooks are stored in the WP ecosystem.

First of all you need to understand where all hooks are stored and how they do it. All hooks for filters and actions are stored in global variable called wp_filter, yes yes action hooks are stored in this variable too. This variable is associated array, where key is the name of action or filter and value is another associative array. For example let's take a look at 'init' action, at this stage we will see following structure:

$wp_filter = array(
    'init' => array(...),
);

This sub array has numeric keys and values as arrays. Numeric keys are our priorities. Arrays, associated with numeric keys, contain a list of hooks with the same priority. So if we call add_action( 'init', 'wpse8170_my_first_init', 20 ), then call add_action( 'init', 'wpse8170_my_second_init', 20 ) and finally call add_action( 'init', 'wpse8170_my_third_init', 10 ), our example will look like:

$wp_filter = array(
    'init' => array(
        20 => array(
            'wpse8170_my_first_init' => array(
                'accepted_args' => 1, // the number of accepted arguments by your hook
                'function' => 'wpse8170_my_first_init', // callback function
            ),
            'wpse8170_my_second_init' => array(...),
        ),
        10 => array(
            'wpse8170_my_third_init' => array(...),
        ),
    ),
);

Now when init action is triggered all hooks will be sorted with usage of ksort function and our array looks now:

    array(
        10 => array(
            'wpse8170_my_third_init' => array(...),
        ),
        20 => array(
            'wpse8170_my_first_init' => array(
                'accepted_args' => 1, // the number of accepted arguments by your hook
                'function' => 'wpse8170_my_first_init', // callback function
            ),
            'wpse8170_my_second_init' => array(...),
        ),
    ),

And all hooks will be executed in this queue: first 'wpse8170_my_third_init', then 'wpse8170_my_first_init' and finally 'wpse8170_my_second_init'.

So you can see that there is no limits and penalties and you can use any value which is acceptable as a key for associated array by your PHP environment.

share|improve this answer
2  
max( $priorities ) + 1 will fail if the last number is equal to PHP_INT_MAX. In that case you have to convert the value to a string and add something to it. –  toscho Jan 9 '13 at 12:21
    
@toscho Yes, agree. Updated the bonus snippet. –  Eugene Manuilov Jan 9 '13 at 12:30
1  
The "bonus" is a bad idea, in case the definition for $wp_filter ever changes. It is not meant to be used directly by plugins. We have made modifications in the past (primarily for performance reasons, incidentally). –  Andrew Nacin Jan 9 '13 at 16:07
    
@AndrewNacin ok, I have removed it, as it causes too many questions :) –  Eugene Manuilov Jan 9 '13 at 16:12
add comment

It's an integer, so on a 32-bit PHP system it will be limited to -2147483648 to 2147483647, and on 64-bit PHP it will be limited to -9223372036854775808 to 9223372036854775807.

Edit: no performance penalty, it's an integer.

But... seriously? :)

share|improve this answer
    
I get that it's an integer, but I was talking about the actual hook mechanism. I've heard people say that having a hook that's too large will cause the hook to fail completely, and the callback will not be executed –  bungeshea Jan 9 '13 at 9:58
    
Who? When? It's just an index into an array, and a sparse array at that, so there's negligible impact. But honestly, big numbers are pretty much an indicator of not understanding a problem (e.g. frantically trying bloody anything that might work!) –  webaware Jan 9 '13 at 10:03
add comment

No limit and there is no performance penalty. From inspecting the code you can even use strings as priorities although I wouldn't recommend doing that ;)

If your action has to be last then you can inspect the assigned pririties by looking at the indexes of the global $wp_actions[your hook] when your action is called, and add it again with higher priority if needed, but I fail to see a reason for actually doing this kind of things.

share|improve this answer
add comment

There is "practically" no limit as hooks are actually stored as arrays and the priority is the numerical index.

But, in reality, the array size will be limited by the amount of memory allocated for the execution of the script.

So, I guess setting a ridiculously big priority number - which just translates to a numerical index in the array where the hooked functions are stored - shouldn't crash wordpress.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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