Is there a limit to hook priority?

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?

Read More

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

Related posts

Leave a Reply

5 comments

  1. 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.

  2. 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? 🙂

  3. @shea – WordPress actions work exactly the OPPOSITE way you assumed. A higher priority figure will NOT override others, and usage of PHP_INT_MAX is NOT some “extreme” attempt to force this action/filter to run before any others.

    To put your action/filter at the TOP of the execution sequence, you’d need to use a priority of at least 0, or better: PHP_INT_MIN.

    PHP_INT_MAX is simply at the opposite end; it’s used when you want your action/filter to run AFTER all other (normal priority) hooks have completed.

  4. 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.

  5. 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.