Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Björn

Taxonomy Field for users

Hi

I registered a custom taxonomy for users. In the profile dialog I can now add categories to my users. Of course I would like to see the assigned users in an admin column. Even better would be, to filter and inline-edit this taxonomy.

For normal posts and custom post types I’ve found this documentation: https://www.admincolumns.com/documentation/how-to/taxonomy-column-sorting-filtering-and-editing/. But it doesn’t work for users. Any ideas or hints, how I could achieve this?

Kind regards
Björn

7 years, 11 months ago
Tobias Schutter
Developer

WordPress doesn’t natively have a UI to link terms to users. Did you use a plugin for that?

7 years, 11 months ago
Björn

Hi Tobias

Yes, you’re almost right. I didn’t use a plugin, but some custom code, which I have from here.

First I register the taxonomy for the user scope with this code:

function register_user_taxonomy(){

	$labels = array(
		'name' => 'User Category',
		'singular_name' => 'User Category',
		'search_items' => 'Search User Categories',
		'all_items' => 'All User Categories',
		'parent_item' => 'Parent User Category',
		'parent_item_colon' => 'Parent User Category',
		'edit_item' => 'Edit User Category',
		'update_item' => 'Update User Category',
		'add_new_item' => 'Add New User Category',
		'new_item_name' => 'New User Category Name',
		'menu_name' => 'User Category'
	);

	$args = array(
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'show_admin_column' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'user_category')
	);

	register_taxonomy( 'user_category' , 'user' , $args );
}
add_action( 'init', 'register_user_taxonomy' );

Then with the following code I can add custom categories to the users in their user profile page:

add_action( 'show_user_profile', 'show_user_category' );
add_action( 'edit_user_profile', 'show_user_category' );
function show_user_category( $user ) {
 
    //get the terms that the user is assigned to 
    $assigned_terms = wp_get_object_terms( $user->ID, 'user_category' );
    $assigned_term_ids = array();
    foreach( $assigned_terms as $term ) {
        $assigned_term_ids[] = $term->term_id;
    }
 
    //get all the terms we have
    $user_cats = get_terms( 'user_category', array('hide_empty'=>false) );
 
    echo "<h3>User Category</h3>";

     //list the terms as checkbox, make sure the assigned terms are checked
    foreach( $user_cats as $cat ) { ?>
        <input type="checkbox" id="user-category-<?php echo $cat->term_id ?>" <?php if(in_array( $cat->term_id, $assigned_term_ids )) echo 'checked=checked';?> name="user_category[]"  value="<?php echo $cat->term_id;?>"/> 
        <?php
    	echo '<label for="user-category-'.$cat->term_id.'">'.$cat->name.'</label>';
    	echo '<br />';
    }
}

add_action( 'personal_options_update', 'save_user_category' );
add_action( 'edit_user_profile_update', 'save_user_category' );
function save_user_category( $user_id ) {

	$user_terms = $_POST['user_category'];
	$terms = array_unique( array_map( 'intval', $user_terms ) );
	wp_set_object_terms( $user_id, $terms, 'user_category', false );

	//make sure you clear the term cache
	clean_object_term_cache($user_id, 'user_category');
}

Do you have some hints, where in your code I could hook in for adding this custom user taxonomy to admin columns?

Kind regards
Björn

7 years, 11 months ago
Tobias Schutter
Developer

At the moment the Taxonomy column check for post_types only. By changing it’s apply_conditional we’ll be able to make it available for users as well. The next step is passing the object_type into get_object_taxonomies to retrieve it’s taxonomies.

We’ll add these changes in to our next release.

Here is the full patch, add them to your code if you like.


class CPAC_Column_Taxonomy extends CPAC_Column {

	public function init() {
		parent::init();

		$this->properties['type'] = 'column-taxonomy';
		$this->properties['label'] = __( 'Taxonomy', 'codepress-admin-columns' );
		$this->properties['is_cloneable'] = true;

		$this->options['taxonomy'] = '';
	}

	/**
	 * @see CPAC_Column::get_value()
	 * @since 2.0
	 */
	public function get_value( $object_id ) {
		$term_ids = $this->get_raw_value( $object_id );

		return $this->get_terms_for_display( $term_ids, $this->get_taxonomy() );
	}

	/**
	 * @see CPAC_Column::get_raw_value()
	 * @since 2.0.3
	 */
	public function get_raw_value( $object_id ) {
		return wp_get_post_terms( $object_id, $this->get_taxonomy(), array( 'fields' => 'ids' ) );
	}

	/**
	 * @see CPAC_Column::get_value()
	 * @since 2.3.4
	 */
	public function get_taxonomy() {
		return $this->get_option( 'taxonomy' );
	}

	private function get_object_type() {
		return $this->get_post_type() ? $this->get_post_type() : $this->get_storage_model_type();
	}

	/**
	 * @see CPAC_Column::apply_conditional()
	 * @since 2.0
	 */
	public function apply_conditional() {
		return get_object_taxonomies( $this->get_object_type() ) ? true : false;
	}

	/**
	 * Display Settings
	 *
	 * @see CPAC_Column::display_settings()
	 * @since 2.0
	 */
	public function display_settings() {
		$taxonomies = get_object_taxonomies( $this->get_object_type(), 'objects' );

		foreach ( $taxonomies as $index => $taxonomy ) {
			if ( $taxonomy->name == 'post_format' ) {
				unset( $taxonomies[ $index ] );
			}
		}
		?>

		<tr class="column_taxonomy">
			<?php $this->label_view( __( "Taxonomy", 'codepress-admin-columns' ), '', 'taxonomy' ); ?>
			<td class="input">
				<select name="<?php $this->attr_name( 'taxonomy' ); ?>" id="<?php $this->attr_id( 'taxonomy' ); ?>">
					<?php foreach ( $taxonomies as $taxonomy ) : ?>
						<option value="<?php echo $taxonomy->name; ?>"<?php selected( $taxonomy->name, $this->get_taxonomy() ) ?>><?php echo $taxonomy->label; ?></option>
					<?php endforeach; ?>
				</select>
			</td>
		</tr>

		<?php
	}
}

This will only display the taxonomies. Editing, sorting and filtering is not yet supported in this code example.

Any updates on this will be posted on our ticket on github.

Cheers, Tobias

7 years, 11 months ago
Björn

Hi Tobias

That is great news! Thank you very much for such a quick improvement. FYI: There is one little bug (not important). As you can see in the image below, when I hover over (1) a wrong edit-link (2) is produced. The correct one would be https://bodenhaftung.ch/wp-admin/edit-tags.php?action=edit&taxonomy=user_category&tag_ID=29 (see line 152 in https://github.com/WordPress/WordPress/blob/master/wp-admin/edit-tags.php).

I’ve seen, that you plan to add editing, sorting and filtering in 3.9. If I could wish: the most important function is sorting, then editing and least important filtering. Just for my client: Will it take days, weeks or months until these improvements are made?

Kind regards
Björn

7 years, 11 months ago
Björn

Here the mentioned image

7 years, 11 months ago
Stefan van den Dungen Gronovius
Developer

Hi Björn,

I’ve added your comment about the edit link to the issue.
The next big release 3.9 is planned for May 23, 2016.
In the meantime we often do smaller releases as you can see on our roadmap page (https://www.admincolumns.com/documentation/faq/roadmap/)

7 years, 11 months ago
simonfairway

Has this not yet been released? Do we still need to add the custom code for user taxonomy columns?

6 years, 2 months ago
Stefan van den Dungen Gronovius
Developer

We had another look at this request.
Since it is not possible to add taxonomies to users by default in WordPress, we decided not to implement this into our core product.

We did create a column to support user taxonomies in our extra column repo on Github:
https://github.com/codepress/ac-addon-extra-columns

This column comes with inline edit support but not with filtering and sorting.

Disclaimer
Feel free to just pick the necessary code from this plugin and integrate it into your own plugin or theme. We do not encourage people to use this plugin on production websites.

6 years ago

You must be logged in to reply to this topic.