I have a site with people from different areas of expertise, and I want to be able to group them with custom taxonomies. I've taken Justin Tadlocks tutorial-code and it works fine when I have one taxonomy registered, but when i register another one they start to behave strangely. For now I have the taxonomies "Profession" and "Office". I have five different professions and 20+ offices. If i select one profession and save, then another two might be checked of even though I haven't checked them.
Could it be a problem when the terms are being saved? Should it be one function to rule them all or should they have separate functions like now?
EDIT: 400 lines of code is a bit much, sorry.
What it does is that it registers two taxonomies and then "bind" them to the user-pages instead of latching on to "Posts" which it does by default, and then multiplied by two. Each taxonomy gets its own function for everything. I don't know if it is the task of saving the fields that is the source of my problems, but everything works just fine until i save a users profile.
Here's the function that saves the checkboxes.
function my_save_user_profession_terms( $user_id ) {
$tax = get_taxonomy( 'profession' );
/* Make sure the current user can edit the user and assign terms before proceeding. */
if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
return false;
$term = $_POST['profession'] ;
/* Sets the terms (we're just using a single term) for the user. */
wp_set_object_terms( $user_id, $term, 'profession', false);
clean_object_term_cache( $user_id, 'profession' );
}
add_action( 'personal_options_update', 'my_save_user_office_terms' );
add_action( 'edit_user_profile_update', 'my_save_user_office_terms' );
And here's how I output my terms as checkboxes in the admin:
/* If there are any profession terms, loop through them and display checkboxes. */
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) { ?>
<input type="checkbox" name="profession[]" id="profession-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, 'profession', $term ) ); ?> /> <label for="profession-<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?></label> <br />
<?php }
}
Edit 2, added answer.