1

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.

Here's my code.

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.

10
  • 1
    It sounds like a problem with how you are saving the data or with how the form fields are getting named, but 393 line of code is a lot to read through. Do you think you can narrow this down and edit the relevant bits into your question?
    – s_ha_dum
    May 31, 2013 at 14:22
  • @s_ha_dum I added the code that saves the terms and then how they are being outputted on the profile page. If I only have one user then the saving seems to work fine, but when i have more than that then boxes start getting checked on random upon profile save. Worth to note i guess is that they never get _un_checked
    – talven
    Jun 1, 2013 at 13:07
  • I have no issues with your code. I pasted the whole thing into a mu-plugin file and it works, as near as I can tell, like a charm. There is something peculiar on your site that hasn't been identified. Maybe it is a problem that shows up when you get a lot of terms (though you don't have that many) but I doubt it as it all managed by the same system that manages categories. Sorry, I don't know hos to duplicate this.
    – s_ha_dum
    Jun 1, 2013 at 16:50
  • @s_ha_dum Thank you for taking your time and testing my code. Did you try with more than one user and change the terms many times back and forth? I've tried the very code i pasted earlier on a completely blank wp-install, with two different themes and with no active plugins and I still get the random checking. My exact process was to register the taxonomies, create about 20 terms per taxonomy, create three users and then start checking boxes and saving, going back and uncheck, save etc. After about 4-5 saves things start to happen.
    – talven
    Jun 1, 2013 at 17:55
  • I did check with two users and a few terms, and saved half a dozen times or more. I will try with more terms and users.
    – s_ha_dum
    Jun 1, 2013 at 17:58

1 Answer 1

2

Thankfully I found an easy solution to this problem. When outputting or terms as checkboxes/radios/whatever, we need to change this:

<?php checked( true, is_object_in_term( $user->ID, 'YOUR-TAX', $term ) ); ?>

To this:

<?php checked( true, is_object_in_term( $user->ID, 'YOUR-TAX', $term->term_id ) ); ?>

This is from the function in my original post. YOUR-TAX is where you put in the name of your taxonomy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.