I have been following a tutorial on setting up a user taxonomy on Justin Tadlock's site. I got everything working but I wanted to adjust the taxonomy from a single radio selection to a multi checkbox selection.
// If there are any competence terms, loop through them and display checkboxes.
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) { ?>
<input type="checkbox" name="competence-<?php echo esc_attr( $term->slug ); ?>" id="competence-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, 'competence', $term ) ); ?> />
<label for="competence-<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?></label> <br />
<?php }
}
I have adjusted the input type and the name value but I could not get the checkbox values to stay checked.
I did some simple testing and hard coded values into the save section
$term = esc_attr( $_POST['competence-photographer'] );
$term = esc_attr( $_POST['competence-server-administrator'] );
And I was able to retain the values if the were checked or not so I created a foreach loop thinking it would loop through all the taxonomy terms but I must be doing something wrong because I cannot git it to save the values
function save_user_competence_terms( $user_id ) {
$tax = get_taxonomy( 'competence' );
if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
return false;
$terms = get_terms( 'competence', array( 'hide_empty' => false ) );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$term = esc_attr( $_POST[$term->slug] );
wp_set_object_terms( $user_id, array( $term ), 'competence', false);
clean_object_term_cache( $user_id, 'competence' );
}
}
}
I believe the method that I am approaching this with is correct but I'm missing something that is not allowing the foreach to read the terms to check for the checked boxes. Any insight would be appreciated.