admin管理员组

文章数量:1316980

I added some custom taxonomy to a CPT with:

register_taxonomy( 'sport', 'activity', [
        'label'     => 'Sport',
        'rewrite'   => [ 'slug' => 'sport'],
        'hierarchical' => true
    ]);

and this works fine. The taxonomy also shows up in the admin menu, as a subpage for activity.

Then I wanted to do the same for the users:

    register_taxonomy( 'team', 'user', [
        'label'     => 'Team',
        'rewrite'   => [ 'slug' => 'team'],
        'hierarchical' => true
    ]);

However this did not work for me, the taxonomy did not show up in the admin menu.
After some research, I could make it appear with this code

function add_user_tax_menu() {
    $tax = get_taxonomy( 'team' );
    add_users_page(
        esc_attr( $tax->labels->menu_name ),
        esc_attr( $tax->labels->menu_name ),
        $tax->cap->manage_terms,
        'edit-tags.php?taxonomy=' . $tax->name
    );
}
add_action('admin_menu', 'add_user_tax_menu');

But it still does not work properly. For example if click on the link in the count column, I end up in the posts sections, with no posts listed. I guess I have to somehow tell WP that the type is user and not post, but I cannot figure out how.

I also tried using 'edit-tags.php?taxonomy=' . $tax->name . '&post_type=user' instead, but that didn't work either (maybe because user isn't a post type?).

Update

With the help of the answer posted by @saqib-ali and the tutorial mentioned in the comment, I got somewhat closer to to a solution. However, I still cannot see which users are assigned to a team. When I click on the link in the Count column (see screenshot) I end up back in the posts section of the admin panel.

The link points to

.../wp-admin/edit.php?team=team-a

My guess is, that it needs to be changed to

.../wp-admin/users.php?team=team-a

according to the mentioned tutorial, this might be done with the manage_circle_custom_column hook. But I could not find a hook like that in the WP doc (only manage_posts_custom_column). So maybe that hook doesn't exist (anymore)?

And then I would probably also need to add a filter hook that filters users by the taxonomy in the query parameter. Similar to how it works for the role

.../wp-admin/users.php?role=administrator 

I couldn't yet find the appropriate hook for either task.

I added some custom taxonomy to a CPT with:

register_taxonomy( 'sport', 'activity', [
        'label'     => 'Sport',
        'rewrite'   => [ 'slug' => 'sport'],
        'hierarchical' => true
    ]);

and this works fine. The taxonomy also shows up in the admin menu, as a subpage for activity.

Then I wanted to do the same for the users:

    register_taxonomy( 'team', 'user', [
        'label'     => 'Team',
        'rewrite'   => [ 'slug' => 'team'],
        'hierarchical' => true
    ]);

However this did not work for me, the taxonomy did not show up in the admin menu.
After some research, I could make it appear with this code

function add_user_tax_menu() {
    $tax = get_taxonomy( 'team' );
    add_users_page(
        esc_attr( $tax->labels->menu_name ),
        esc_attr( $tax->labels->menu_name ),
        $tax->cap->manage_terms,
        'edit-tags.php?taxonomy=' . $tax->name
    );
}
add_action('admin_menu', 'add_user_tax_menu');

But it still does not work properly. For example if click on the link in the count column, I end up in the posts sections, with no posts listed. I guess I have to somehow tell WP that the type is user and not post, but I cannot figure out how.

I also tried using 'edit-tags.php?taxonomy=' . $tax->name . '&post_type=user' instead, but that didn't work either (maybe because user isn't a post type?).

Update

With the help of the answer posted by @saqib-ali and the tutorial mentioned in the comment, I got somewhat closer to to a solution. However, I still cannot see which users are assigned to a team. When I click on the link in the Count column (see screenshot) I end up back in the posts section of the admin panel.

The link points to

.../wp-admin/edit.php?team=team-a

My guess is, that it needs to be changed to

.../wp-admin/users.php?team=team-a

according to the mentioned tutorial, this might be done with the manage_circle_custom_column hook. But I could not find a hook like that in the WP doc (only manage_posts_custom_column). So maybe that hook doesn't exist (anymore)?

And then I would probably also need to add a filter hook that filters users by the taxonomy in the query parameter. Similar to how it works for the role

.../wp-admin/users.php?role=administrator 

I couldn't yet find the appropriate hook for either task.

Share Improve this question edited Nov 3, 2020 at 9:56 jost21 asked Nov 1, 2020 at 22:16 jost21jost21 2592 silver badges11 bronze badges 2
  • note that WP doesn't provide a UI for user taxonomies, but adding it isn't difficult, take a look at justintadlock/archives/2011/10/20/… – Tom J Nowell Commented Nov 2, 2020 at 0:31
  • This is an excellent tutorial for this, it's a little dated and the comments seem to be now disabled. One of the crucial steps in the tutorial refers to a comment by "James". I checked archive and that comment basically suggests the hook/function posted by @saqib-ali on this question here. The tutorial didn't solve my issue completely, but at least pointed me in the right direction. – jost21 Commented Nov 3, 2020 at 9:31
Add a comment  | 

1 Answer 1

Reset to default 1

There comes a hook in wordpress named Parent File (Filters the parent file of an admin menu sub-menu item) read more about it here

In your case it goes like:

add_filter('parent_file', 'parent_menu');

function parent_menu($parent = '') {
    global $pagenow;
    
    if(!empty($_GET['taxonomy']) && $pagenow == 'edit-tags.php' && $_GET['taxonomy'] == 'category') {
        $parent = 'edit.php';
    }
     /*If we're editing one of the user taxonomies
    We must be within the users menu, so highlight that*/
    if(!empty($_GET['taxonomy']) && $pagenow == 'edit-tags.php' && $_GET['taxonomy'] == 'team') {
        $parent = 'users.php';
    }
    
    return $parent;
}

Hope that it solves your problem.

本文标签: How to add taxonomy to user