actions - How to know what priority to use with add_action()?

I was writing a small plugin to remove some menu items for non-admin users from the backend, and discovered that my plug

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?

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?

Share Improve this question asked Jun 23, 2015 at 16:45 j08691j08691 7952 gold badges12 silver badges27 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

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.

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.

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/Function_Reference/has_action

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744785622a4593607.html

相关推荐

  • actions - How to know what priority to use with add_action()?

    I was writing a small plugin to remove some menu items for non-admin users from the backend, and discovered that my plug

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信