16

I was writing a small plugin to remove some menu items for non-admin users from the backend, and discovered that my plugin didn't do anything unless I specified a priority in my code:

add_action('admin_bar_menu', 'remove_toolbar_items', 999);

Without the 999, the code doesn't remove the items in my remove_toolbar_items function, and with it it works great:

function remove_toolbar_items( $wp_admin_bar ) {
    if ( !current_user_can( 'manage_options' ) ) {
        $wp_admin_bar->remove_node('new-post');
        $wp_admin_bar->remove_node('comments');
    }
}

The docs for the priority parameter state:

Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default value: 10

However I didn't find anything that explains how you're supposed to determine what priority to use. How do you determine when to use priority, and what priority to use? I feel like I could've been scratching my head for hours if I hadn't toyed around with the priority parameter.

Also, I see that the default priority is 10, but is there a known range of priority values?

7

range of priority values? range of priority values?

Generally, speaking, you can't know a priori what priority something is hooked with. The priority needed depends on how other callbacks where hooked in. Often that is the default value of 10 but it could be anywhere between PHP_INT_MIN (negative integers are still integers) and PHP_INT_MAX and there is no way to be sure except by experiment and, if possible (as with the Core and any themes or plugins that you are specifically concerned with), by examining the source.

2
  • 1
    Thanks. Seems like the core should have some little built-in utility to give you this information.
    – j08691
    Jun 24 '15 at 19:34
  • That's not correct information and can't be answer. Mohammed Asif answered correctly. has_filter('init', 'some_hook') allows to know what priority action has. Apr 27 '20 at 7:05
7

WordPress puts your actions into an array with an indexed priorities. You can see this by printing out ( in the admin panel admin_init ) $wp_filter:

*Note* as @s_ha_dum points out in the comments below, admin_init may not catch all added hooks into the action, the more reliable print out may be hooking into shutdown instead.

function filter_print() {
    global $wp_filter;
    print_r( $wp_filter['admin_bar_menu'] );
    die();
}
add_action( 'admin_init', 'filter_print' );

This gives us a neat array that looks something like this: ( simplified )

Array(
    [admin_bar_menu] => Array (
        [0] => Array (
            [wp_admin_bar_my_account_menu] => Array (
                [function] => wp_admin_bar_my_account_menu
                [accepted_args] => 1
            )
            [wp_admin_bar_sidebar_toggle] => Array (
                [function] => wp_admin_bar_sidebar_toggle
                [accepted_args] => 1
            )
        )

        [4] => Array (
            [wp_admin_bar_search_menu] => Array (
                [function] => wp_admin_bar_search_menu
                [accepted_args] => 1
            )
        )

        [7] => Array (
            [wp_admin_bar_my_account_item] => Array (
                [function] => wp_admin_bar_my_account_item
                [accepted_args] => 1
            )
        )

        [10] => Array (
            [wp_admin_bar_wp_menu] => Array (
                [function] => wp_admin_bar_wp_menu
                [accepted_args] => 1
            )
        )

        [20] => ...
    )
)

The 0, 4, 7, 10, and so forth are the priorities of the actions, when a new action is added it's defaulted to 10, similar to index 0 in the example above, they are just stacked into the same index of the array. Considering that many hooks are added into this particular action you would want to at the very end or at last after a specific action was run ( such as menus ). 1 of the two priorities could also work just as effectively: 81 or 201.

For the most part, the default priority of 10 is sufficient enough. Other times you want to add your hook direct after another ( to maybe nullify it's purpose or remove a specific item ) at which case you can use the global $wp_filter; to figure out where it needs to go.

3
  • I thought about this, but it is only going to show things hooked in before or on admin_init and only things hooked in on the current install. It says nothing about what an as yet uninstalled plugin or theme might do. +1 anyway.
    – s_ha_dum
    Jun 23 '15 at 17:29
  • @s_ha_dum That's a good point, I imagine you could also use something like admin_footer though right? Or would that be the same issue?
    – Howdy_McGee
    Jun 23 '15 at 17:31
  • 1
    The shutdown hook would be your best bet, but only for installed code and even then, things can be conditionally hooked so it may be possible to miss some anyway.
    – s_ha_dum
    Jun 23 '15 at 17:34
6

Well, there's a way to find the priority of an action.

we can use the following codehas_action( $tag, $function_to_check ) which Optionally returns the priority on that hook for the specified function.

Ref: https://codex.wordpress.org/Function_Reference/has_action

-1

Just in case someone is looking for WordPress Action Priority/Reference list, the full hook link is here:

https://codex.wordpress.org/Plugin_API/Action_Reference/

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.