最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp admin - Add and Remove Row Actions in an Existing WP_List_Table

programmeradmin1浏览0评论

I would like to modify the row actions of an existing WP_List_Table. For example, in the Users section in the back end, I would like to remove the "Edit" option from each user.

I would like to also add new actions, such as "Books", which would list the books read by this user (this works with another plugin).

I have a feeling it is some sort of filter, but could not find anything in documentation.

Please note that I am not creating a new table. This is an existing WordPress table (e.g., Users).

How do I do that?

Thanks.

I would like to modify the row actions of an existing WP_List_Table. For example, in the Users section in the back end, I would like to remove the "Edit" option from each user.

I would like to also add new actions, such as "Books", which would list the books read by this user (this works with another plugin).

I have a feeling it is some sort of filter, but could not find anything in documentation.

Please note that I am not creating a new table. This is an existing WordPress table (e.g., Users).

How do I do that?

Thanks.

Share Improve this question asked Jan 27, 2021 at 14:40 GreesoGreeso 2,2247 gold badges33 silver badges57 bronze badges 3
  • 1 Have a look at user_row_actions which is specifically for the WP_Users_List_Table class which extends WP_List_Table. – Sally CJ Commented Feb 12, 2021 at 5:56
  • 1 @SallyCJ - Thanks. Maybe you should add this as an answer so I can accept it? – Greeso Commented Feb 12, 2021 at 14:31
  • Done, @Greeso. :) – Sally CJ Commented Feb 13, 2021 at 4:45
Add a comment  | 

1 Answer 1

Reset to default 2

As for the user rows in the Users list table (at wp-admin/users.php), you would use the user_row_actions hook like so:

add_filter( 'user_row_actions', 'my_user_row_actions', 10, 2 );
function my_user_row_actions( $actions, $user_object ) {
    // Remove the Edit action.
    unset( $actions['edit'] );

    // Add your custom action.
    $actions['my_action'] = '<a href="<action URL>">Action</a>';

    return $actions;
}

The other WordPress list tables which extend the WP_List_Table class also fire a similar hook with the same naming convention, i.e. <text>_row_actions, and here are the other available hooks list as of writing:

Note: These hooks mostly have only two parameters — $actions (an array of action links) and an object representing a post, term, user or comment — but the media_row_actions has a third parameter named $detached (see the docs for more details).

  • page_row_actions — fires in the Pages list table (page post type); second param: $post (a WP_Post instance)

  • post_row_actions — fires in the Posts list table (post and custom post types); second param: $post (a WP_Post instance)

    Note that this hook and the one above, they are fired in the same list table class which is WP_Posts_List_Table.

  • media_row_actions — fires in the Media list table; second param: $post (a WP_Post instance)

  • comment_row_actions — fires in the Comments list table (which is also used in the Recent Comments dashboard widget); second param: $comment (a WP_Comment instance)

  • ms_user_row_actions — fires in the Network (or Multisite) Admin Users list table; second param: $user (a WP_User instance)

  • <taxonomy>_row_actions — fires in the Terms list table ( for Tags (post_tag), Categories (category) and custom taxonomies ); second param: $tag (a WP_Term instance)

  • tag_row_actions — same as above, i.e. for any taxonomy — but if you want to target a specific taxonomy, you might want to use the above hook instead

发布评论

评论列表(0)

  1. 暂无评论