How to Add User Taxonomy in WordPress

   Back to list

Introduction

In one of our projects, I added the ability to assign users to a category and displayed a list of these categories, once they had users in them, on the front-end. I found plenty tutorials for that but I noticed that none of them provided an accurate way to count users when they are added or removed from the term. This could be simply done with an additional query that would count the category’s users when listing the categories on the front-end but this is a pretty poor solution when there is a growing number of taxonomy terms and users.  Here is how I found a better solution.

Motivation

As a blog’s administrator, I should be able to manage authors categories and choose categories for authors. As an author, I should be able to choose categories on my profile page. On the front-end, a visitor should be able to see the list of categories with the number of assigned authors and listed authors in case the category contains any user.

The beginning

In the ‘inc’ theme’s directory, I created a ‘user-category-taxonomy.php’ file where I’ll add all the code shown in this tutorial. This file needs to be called in functions.php file with the below snippet:

Let’s do some coding

Below I define the taxonomy name and user category meta key in our file:

Now it’s time to register users taxonomy with the below hook that calls ‘nopio_register_user_category_taxonomy’ function:

As you can see a default ‘update_count_callback’ is not used because it does not work for counting users in user taxonomy. You can find more about counting items in taxonomies here in the Parameters section. I created a custom function for this purpose but we’ll get back to this one later. For now, let’s focus on adding an admin page for managing categories.

To make it happen, we’ll use the below hook that calls ‘nopio_add_user_categories_admin_page’ function:

If you refresh your admin page and click on the users’ menu you’ll see that the last sub-menu link is named ‘User Categories’ so here we have it.

WordPress User Taxonomy Screen 1

The strange thing is that after clicking on the link you will see the manage ‘User Category’ page but in the menu the Posts link is highlighted.

WordPress User Taxonomy Screen 2

Let’s fix that with the below code:

Refresh the page and you’ll see that now it’s better.

WordPress User Taxonomy Screen 3

Now we can add a few categories and go to the users page to create a few authors and subscribes that we’ll use for testing. After that, we need to show a multi-select field for categories and be sure it’s shown only on author’s profile page in case the profile is edited by admin or author. For that purpose we’ll use two hooks, the first one allows to show the select when the author is reviewing his profile page and the second one allows to view author’s profile by other users. Both calls the same function ‘nopio_admin_user_profile_category_select’.

In the function, we need to be sure to show the select field for admins and authors only. In the next step, let’s add a new HTML table with the multi-select field. This table will be shown as the last one on the author’s profile page. In addition, the multi-select field is created with a nopio_custom_form_select custom helper.

Here’s the select helper function, I won’t focus on this part l but it can be used for creating single and multi-select fields:

Great, now please try to edit any author’s profile logged in as admin, author or subscriber. You should be able to see categories multi-select and only change the selection when logged in as admin or as an author.

WordPress User Taxonomy Screen 4

This is the right time to add a code responsible for saving and updating the chosen categories. With the first hook author will be able to save chosen categories when saving his profile and the second one allows to save authors profile for other users. In both cases, we’ll use the same function ‘nopio_admin_save_user_categories’. Again we need to be sure that only admins and authors can save author profile. After that, we’re getting new categories from the $_POST. Here you could add a validation for sent categories’ ids, I’ll skip it here.

In the next step, we need to get users’ current categories’ ids. As the last step in case admin changes user’s role let’s delete all user’s categories meta data and re-count categories from which the user was removed. In the second case, the user’s role isn’t changed then let’s update user’s category meta data with a new categories’ ids and also re-count categories from which the user was removed or which he was added to.

As I mentioned before, the function ‘nopio_update_users_categories_count’ is replacing the functionality from not used ‘update_count_callback’ during user category taxonomy registration. Here’s how it works, firstly we need to merge ids from previous and new categories into one array. Next, let’s recount authors for each category in a foreach loop. We can do that with a SELECT query. In the DB the meta count value looks like this a:2:{i:0;s:1:”2″;i:1;s:1:”3″;} that’s why we need to use LIKE in the select query. As the final step, we can update category count DB field.

We’re ready to show our category list to their authors. You can call this function in your template file or home page ‘front-page.php’ file. First, we’ll get all not empty categories and after that, we can show authors for that category.

Here is the result on the front-end page, in my case ‘Category 3’ was an empty and it’s not listed on the page:

WordPress User Taxonomy Screen 5

Summary

In this tutorial, we learned how to create a custom taxonomy for a chosen user type and count added and removed users for a chosen taxonomy term.

In the User Category page, you’ll see that the count’s number link does not work as expected but that would be a subject for a different tutorial.

The code can be found here on GitHub.

Hope this was helpful, please comment and let me know if you have any suggestions.

Send this to a friend