• seforoth

    (@seforoth)


    Hi,

    There is an official patch to allow user taxonomies. The patch unfortunately does not yet show taxonomy options within the user profiles, but here is the code which appends all ‘user’ specific taxonomies to the user profile. The code below may not cover every scenario, but it allows you to further mod it to suit your own needs.

    PATCH

    https://core.trac.wordpress.org/ticket/31383

    CODE SHOW/SAVE USER TAXONOMIES

    /**
     * Show taxonomies in user profile (admin)
     */
    function admin_user_profile_taxonomies( $user ) {
    	$taxonomies = get_object_taxonomies('user');
    	?>
    	<h3>Taxonomies</h3>
    
    	<table class="form-table">
    		<?php foreach ( $taxonomies as $taxonomy ) : ?>
    		<tr>
    			<th><label for="<?php echo $taxonomy; ?>_profile"><?php echo $taxonomy; ?></label></th>
    			<td>
    				<select name="<?php echo $taxonomy; ?>_profile[]" multiple="true">
    					<?php
    					$terms = get_terms( $taxonomy, 'orderby=term_order&order=asc&hide_empty=0' );
    					$user_terms = get_the_author_meta( $taxonomy.'_profile', $user->ID );
    					?>
    					<?php $i=0; foreach ( $terms as $term ) : ?>
    					<option value="<?php echo $term->term_id; ?>" <?php echo in_array( $term->term_id, $user_terms ) ? 'selected' : ''; ?>><?php echo $term->name; ?></option>
    					<?php $i++; endforeach; ?>
    				</select>
    			</td>
    		</tr>
    		<?php endforeach; ?>
    	</table>
    	<?php
    }
    
    add_action( 'show_user_profile', 'admin_user_profile_taxonomies' );
    add_action( 'edit_user_profile', 'admin_user_profile_taxonomies' );
    
    /**
     * Save taxonomies in user profile (admin)
     */
    function admin_save_user_profile_taxonomies( $user_id ) {
    	$taxonomies = get_object_taxonomies('user');
    
    	foreach ( $taxonomies as $taxonomy ) {
    		update_user_meta( $user_id, $taxonomy.'_profile', $_POST[$taxonomy.'_profile'] );
    	}
    }
    
    add_action( 'personal_options_update', 'admin_save_user_profile_taxonomies' );
    add_action( 'edit_user_profile_update', 'admin_save_user_profile_taxonomies' );

    https://wordpress.org/plugins/lh-user-taxonomies/

The topic ‘Alternative solution for user taxonomies’ is closed to new replies.