Last Updated:

Taxonomy for WordPress Users

Hello. In one of my WordPress projects, there was a task for users to spin tags. Like nothing complicated. I poked around in WordPress, rummaged through the Internet and realized that this is not provided for by the standard functionality. Okay, I went to see the plugins. I found them, but they haven't been updated or tested in a very long time. And the project is already working, so I decided not to risk it. Therefore, I decided to screw this functionality to the WordPress theme.

 

And, right. For example, I'll use the standard WordPress theme "Twentyfifteen". And the first thing we will do is in the inc directory of our theme we will place the user-category-taxonomy file.php, in which we will describe our functionality. And let's connect our file to functions.php our theme:

require ( 'inc/user-category-taxonomy.php' );

Let's define a name and key for our taxonomy:

define( 'USER_CATEGORY_NAME', 'user_category');

define( 'USER_CATEGORY_META_KEY', '_user_category');

Once again, we will write all the code for implementing taxonomy for users in the user-category-taxonomy file.php

Now let's register the user's taxonomy using the "init" hook, which will call the productivecode_register_user_category_taxonomy function and create this function

add_action( 'init', 'productivecode_register_user_category_taxonomy' );

function productivecode_register_user_category_taxonomy() {

register_taxonomy(

USER_CATEGORY_NAME, //taxonomy name

'user', //object for which the taxonomy is created

array( //taxonomy details

'public' => true,

'labels' => array(

'name'                  => 'User Tag',

'singular_name'               => 'User Tag',

'menu_name'   => 'User Tags',

'search_items'  => 'Search User Tag',

'popular_items' => 'Popular User Tag',

'all_items'           => 'All User Tag',

'edit_item'         => 'Edit User Tag',

'update_item'   => 'Update User Tag',

'add_new_item'              => 'Add New User Tag',

'new_item_name'          => 'New User Tag Name',

),

'update_count_callback' => function() {

return; //important

}

)

);

}

You can read about the register_taxonomy function here

The next step is to add a page in the admin panel to manage the user's taxonomy. To do this, use the hook "admin_menu", which will call the productivecode_add_user_categories_admin_page function.

add_action( 'admin_menu', 'productivecode_add_user_categories_admin_page' );

function productivecode_add_user_categories_admin_page() {

$taxonomy = get_taxonomy( USER_CATEGORY_NAME );

add_users_page(

esc_attr( $taxonomy->labels->menu_name ),//page title

esc_attr( $taxonomy->labels->menu_name ),//menu title

$taxonomy->cap->manage_terms,//capability

'edit-tags.php?taxonomy=' . $taxonomy->name//menu slug

);

}

Here we used the get_taxonomy function and the add_users_page function.

Now, if we go to the admin panel, we will see:

Let's fix the link a little with the "submenu_file" filter

add_filter( 'submenu_file', 'productivecode_set_user_category_submenu_active' );

function productivecode_set_user_category_submenu_active( $submenu_file ) {

global $parent_file;

if( 'edit-tags.php?taxonomy=' . USER_CATEGORY_NAME == $submenu_file ) {

$parent_file = 'users.php';

}

return $submenu_file;

}

All. Now, when we click on the link, we will be taken to the user's taxonomy management page.

It's cool. Now we can add a few tags for the test.

The next step is to display a tagged field on the user's page. But here the nuance of this field should be displayed only on the author's page or on the page, if it is edited by the author or administrator.

To do this, we will use two hooks "show_user_profile" and "edit_user_profile".

add_action( 'show_user_profile', 'productivecode_admin_user_profile_category_select' );

add_action( 'edit_user_profile', 'productivecode_admin_user_profile_category_select' );

function productivecode_admin_user_profile_category_select( $user ) {

$taxonomy = get_taxonomy( USER_CATEGORY_NAME );

if ( !user_can( $user, 'author' ) ) {

return;

}

?>

<table class="form-table">

<tr>

<th>

<label for="<?php echo USER_CATEGORY_META_KEY ?>">User Tag</label>

</th>

<td>

<?php

$user_category_terms = get_terms( array(

'taxonomy' => USER_CATEGORY_NAME,

'hide_empty' => 0

) );

$select_options = array();

foreach ( $user_category_terms as $term ) {

$select_options[$term->term_id] = $term->name;

}

$meta_values = get_user_meta( $user->ID, USER_CATEGORY_META_KEY, true );

echo productivecode_custom_form_select(

USER_CATEGORY_META_KEY,

$meta_values,

$select_options,

'',

array( 'multiple' =>'multiple' )

);

?>

</td>

</tr>

</table>

<?php

}

function productivecode_custom_form_select( $name, $value, $options, $default_var ='', $html_params = array() ) {

if( empty( $options ) ) {

$options = array( '' => '---choose---');

}

$html_params_string = '';

if( !empty( $html_params ) ) {

if ( array_key_exists( 'multiple', $html_params ) ) {

$name .= '[]';

}

foreach( $html_params as $html_params_key => $html_params_value ) {

$html_params_string .= " {$html_params_key}='{$html_params_value}'";

}

}

echo "<select name='{$name}'{$html_params_string}>";

foreach( $options as $options_value => $options_label ) {

if( ( is_array( $value ) && in_array( $options_value, $value ) )

|| $options_value == $value ) {

$selected = " selected='selected'";

} else {

$selected = '';

}

if( empty( $value ) && !empty( $default_var ) && $options_value == $default_var ) {

$selected = " selected='selected'";

}

echo "<option value='{$options_value}'{$selected}>{$options_label}</option>";

}

echo "</select>";

}

Let's see what happens. Go to the user profile.

Now we have to implement a mechanism for saving tags. To do this, we will use two hooks "personal_options_update" and "edit_user_profile_update"

add_action( 'personal_options_update', 'productivecode_admin_save_user_categories' );

add_action( 'edit_user_profile_update', 'productivecode_admin_save_user_categories' );

function productivecode_admin_save_user_categories( $user_id ) {

$tax = get_taxonomy( USER_CATEGORY_NAME );

$user = get_userdata( $user_id );

if ( !user_can( $user, 'author' ) ) {

return false;

}

$new_categories_ids = $_POST[USER_CATEGORY_META_KEY];

$user_meta = get_user_meta( $user_id, USER_CATEGORY_META_KEY, true );

$previous_categories_ids = array();

if( !empty( $user_meta ) ) {

$previous_categories_ids = (array)$user_meta;

}

if( ( current_user_can( 'administrator' ) && $_POST['role'] != 'author' ) ) {

delete_user_meta( $user_id, USER_CATEGORY_META_KEY );

productivecode_update_users_categories_count( $previous_categories_ids, array() );

} else {

update_user_meta( $user_id, USER_CATEGORY_META_KEY, $new_categories_ids );

productivecode_update_users_categories_count( $previous_categories_ids, $new_categories_ids );

}

}

function productivecode_update_users_categories_count( $previous_terms_ids, $new_terms_ids ) {

global $wpdb;

$terms_ids = array_unique( array_merge( (array)$previous_terms_ids, (array)$new_terms_ids ) );

if( count( $terms_ids ) < 1 ) { return; }

foreach ( $terms_ids as $term_id ) {

$count = $wpdb->get_var(

$wpdb->prepare(

"SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value LIKE %s",

USER_CATEGORY_META_KEY,

'%"' . $term_id . '"%'

)

);

$wpdb->update( $wpdb->term_taxonomy, array( 'count' => $count ), array( 'term_taxonomy_id' => $term_id ) );

}

}

That's it, now we can add and update tags for users. To display users by tag, you must paste the following code:

function productivecode_show_authors_in_categories() {

$categories = get_terms(array(

'taxonomy' => USER_CATEGORY_NAME,

'hide_empty' => true

));

echo '<ul>';

foreach( $categories as $category ) {

echo '<li>';

echo $category->name;

echo " (#{$category->count})";

$args = array(

'role' => 'Author',

'meta_key'         => USER_CATEGORY_META_KEY,

'meta_value'     => '"' . $category->term_id . '"',

'meta_compare'              => 'LIKE'

);

$authors = new WP_User_Query( $args );

echo '<ul>';

foreach( $authors->results as $author ) {

echo '<li>';

echo $author->display_name;

echo '</li>';

}

echo '</ul>';

echo '</li>';

}

echo '</ul>';

}

That's it.

Conclusion.

You and I have done the addition of tags for users in the administrative part of WordPress. Users can then be assigned these tags.