Skip to content

Instantly share code, notes, and snippets.

@wpmark
Created June 11, 2021 11:05
Show Gist options
  • Save wpmark/450234e4a85a77705ab012c5c27ea146 to your computer and use it in GitHub Desktop.
Save wpmark/450234e4a85a77705ab012c5c27ea146 to your computer and use it in GitHub Desktop.
A WordPress plugin that adds categories to users. It allows you to assign categories to users which you could then use elsewhere in your site.
<?php
/*
Plugin Name: User Categories
Plugin URI: https://highrise.digital/
Description: Adds categories to users. It allows you to assign categories to users which you could then use elsewhere in your site.
Version: 1.0
License: GPL-2.0+
Author: Highrise Digital Ltd
Author URI: https://highrise.digital/
Text domain: hd-user-categories
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Registers the user category taxonomy with WordPress.
*/
function hd_register_user_category_taxonomy() {
// create the audience taxonomy.
register_taxonomy(
'user_category',
'user',
array(
'public' => false,
'labels' => array(
'name' => __( 'User Categories ', 'hd-user-category' ),
'singular_name' => __( 'User Category', 'hd-user-category' ),
'menu_name' => __( 'User Category', 'hd-user-category' ),
'search_items' => __( 'Search User Categories', 'hd-user-category' ),
'popular_items' => __( 'Popular User Categories', 'hd-user-category' ),
'all_items' => __( 'All User Categories', 'hd-user-category' ),
'edit_item' => __( 'Edit User Category', 'hd-user-category' ),
'update_item' => __( 'Update User Category', 'hd-user-category' ),
'add_new_item' => __( 'Add New User Category', 'hd-user-category' ),
'new_item_name' => __( 'New User Category', 'hd-user-category' ),
'separate_items_with_commas' => __( 'Separate user categories with commas', 'hd-user-category' ),
'add_or_remove_items' => __( 'Add or remove user categories', 'hd-user-category' ),
'choose_from_most_used' => __( 'Choose from the most popular user categories', 'hd-user-category' ),
),
'show_ui' => true,
'show_in_menu' => true,
'show_admin_column' => false,
'hierarchical' => true,
'show_in_rest' => true,
'rewrite' => array(
'slug' => __( 'team-category', 'hd-user-category' ),
'with_front' => false,
),
)
);
}
add_action( 'init', 'hd_register_user_category_taxonomy' );
/**
* Add an admin page for our user taxonomy as there is not a default one provided.
*/
function hd_register_user_category_taxonomy_admin_page() {
// get the taxonomy object for the user category.
$tax = get_taxonomy( 'user_category' );
// add a new admin page under users to display our taxonomy.
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', 'hd_register_user_category_taxonomy_admin_page' );
/**
* Update parent file name to fix the selected menu issue
*/
function hd_fix_user_category_admin_parent_file( $parent_file ) {
global $submenu_file;
// if we have a taxonomy GET parameter in the url.
if ( ! empty( $_GET['taxonomy'] ) ) {
// if the taxonomy get parameter is our user category taxonomy and we are on the user category edit page.
if ( 'user_category' === $_GET['taxonomy'] && 'edit-tags.php?taxonomy=user_category' === $submenu_file ) {
// set the admin parent page to the user.php page.
$parent_file = 'users.php';
}
}
// return the maybe modified parent file.
return $parent_file;
}
add_filter( 'parent_file', 'hd_fix_user_category_admin_parent_file' );
/**
* Edits the columns for the user category management page.
*
* @param array $columns The current array of registered columns.
* @return array $columns The modified array of registered columns.
*/
function hd_manage_user_category_column( $columns ) {
// remove the posts count column - better to call it users.
unset( $columns['posts'] );
// add in a users column.
$columns['users'] = __( 'Users', 'hd-user-category' );
// return the modified columns.
return $columns;
}
add_filter( 'manage_edit-user_category_columns', 'hd_manage_user_category_column', 10, 1 );
/**
* Adds the correct term count based on the users with that term.
*
* @param string $display WP just passes an empty string here.
* @param string $column The name of the custom column.
* @param int $term_id The ID of the term being displayed in the table.
*/
function hd_output_user_category_column( $display, $column, $term_id ) {
// if this is the users column we created.
if ( 'users' === $column ) {
// get the current term object.
$term = get_term( $term_id, 'user_category' );
// output the term count.
echo esc_html( $term->count );
}
}
add_filter( 'manage_user_category_custom_column', 'hd_output_user_category_column', 10, 3 );
/**
* Adds the form for users to choose their user category.
* Added on the new and edit users screens.
*
* @param mixed WP_User/string The current user object or the current page string.
*/
function hd_user_category_form( $user ) {
// get the taxonomy object for user category.
$tax = get_taxonomy( 'user_category' );
// check the current logged in user can assign user category terms.
if ( ! current_user_can( $tax->cap->assign_terms ) ) {
return;
}
// get all user category terms - the categories!
$user_categories = get_terms(
'user_category',
array(
'hide_empty' => false // show terms that have no user assigned.
)
);
?>
<table class="form-table">
<tr>
<th><label for="user_category"><?php esc_html_e( 'User Categories', 'hd-user-category' ); ?></label></th>
<td>
<fieldset>
<?php
// if we have user categories available.
if ( ! empty( $user_categories ) && ! is_wp_error( $user_categories ) ) {
// loop through each user category.
foreach ( $user_categories as $user_category ) {
// set a checked var to uncheck this term.
$checked = false;
// if the user var is telling us we are on the add new user page.
if ( ! empty( $user->ID ) ) {
// set the checked var based on whether the user is already assigned to this term.
$checked = is_object_in_term( $user->ID, 'user_category', $user_category->term_id );
}
?>
<label for="user_categories-<?php echo esc_attr( $user_category->slug ); ?>">
<input type="checkbox" name="user_category[]" id="user_category-<?php echo esc_attr( $user_category->slug ); ?>" value="<?php echo esc_attr( $user_category->term_id ); ?>"<?php checked( true, $checked ); ?>>
<?php echo esc_html( $user_category->name ); ?>
</label>
<br />
<?php
}
}
?>
</fieldset>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'hd_user_category_form', 10, 1 );
add_action( 'edit_user_profile', 'hd_user_category_form', 10, 1 );
add_action( 'user_new_form', 'hd_user_category_form', 10, 1 );
/**
* Saves the user category selected.
*
* @param integer $user_id The current user ID of the user being edited or added.
*/
function hd_save_user_categories( $user_id ) {
// get the taxonomy object for user category.
$tax = get_taxonomy( 'user_category' );
// check the current logged in user can assign user category terms.
if ( ! current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) ) {
return false;
}
// remove all the relationships to start.
wp_delete_object_term_relationships( $user_id, 'user_category' );
// if we have posted categories.
if ( ! empty( $_POST['user_category'] ) ) {
// set the user categories selected.
wp_set_object_terms(
$user_id,
array_map( 'absint', $_POST['user_category'] ),
'user_category',
false
);
// clean the object term cache for this user.
//clean_object_term_cache( $user_id, 'user_category' );
}
}
add_action( 'personal_options_update', 'hd_save_user_categories', 10, 1 );
add_action( 'edit_user_profile_update', 'hd_save_user_categories', 10, 1 );
add_action( 'user_register', 'hd_save_user_categories', 10, 1 );
@wpmark
Copy link
Author

wpmark commented Apr 6, 2022

@sns192 I think you are better asking the Jet Smart Filters plugin support about this one!

@sns192
Copy link

sns192 commented Apr 6, 2022

No no! Their plugin is working fine, I have tested with other taxonomies and fields.

My question, is what additional changes will we have to make it in the code to make it query compatible in the frontend?

Thanks in advance.

@wpasia
Copy link

wpasia commented Sep 25, 2022

Hi,

I used your plugin and it is working fine. This plugin was what I needed.

But I have a change request.

Can you change checkbox to two select option, one for parent and one for the child?

thanks

@wpasia
Copy link

wpasia commented Sep 27, 2022

I changed it to optgroup select type
🙂

@wpasia
Copy link

wpasia commented Sep 27, 2022

@sns192 Jet Smart Filters can't find it because group isn't store in user_meta table.

you should change line 271 to save as user_meta instead of taxonomy

use add_user_meta or update_user_meta

@svess
Copy link

svess commented Oct 7, 2022

@sns192 Were you able to come up with a solution by chance? I am having the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment