1

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.

2 Answers 2

1

There are two errors in your code.

First one is the inconsistency in field names. Here’s your checkbox field:

 <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 ) ); ?> />

And here’s the part you’re checking it:

foreach ( $terms as $term ) { $term = esc_attr( $_POST[$term->slug] );

So your field has name competence-<TERM_SLUG>, but you’re checking it using only <TERM_SLUG>.

It should be:

foreach ( $terms as $term ) {
    $term = esc_attr( $_POST[ 'competence-' . $term->slug] );

Second problem is the way you’re setting the terms for object:

wp_set_object_terms( $user_id, array( $term ), 'competence', false);

Take a look at  wp_set_object_terms docs. This is the default call for this function:

 wp_set_object_terms( $object_id, $terms, $taxonomy, $append );

As you can see, the last param is $append and it tell if the term should be appended to the existing ones or should it overwrite existing ones. You pass false as the value for that param, so your foreach loop will overwrite existing terms - so only the last one will be saved.

One way to fix it would be like this:

$terms = array();
foreach ( $terms as $term ) {
    $term = esc_attr( $_POST[ 'competence-' . $term->slug] );
    $terms[] = $term;
}
wp_set_object_terms( $user_id, $terms, 'competence', false);
1
  • I'm still having the same issue and apologies for the 'competence-' concatenation. That was an oversite as I posted to the board for help. I have applied the changes you outlined without success. After each save the checkboxes all become unchecked. I will post below my updated code. Commented Feb 27, 2019 at 17:30
0

I gather that in your changes you are trying to show me that the content is saved in an array and to be very honest I do not understand what wp_set_object_terms or clean_object_term_cache do. The majority of this code came from another site and I am just adjust for the checkboxes and doing a poor job. Any additional help based on my about question and the changes applied to the response would be appreciated.

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 ) ) {

    $terms = array();
    foreach ( $terms as $term ) {
    $term = esc_attr( $_POST['competence-' . $term->slug] );
    $terms[] = $term;
    }

    wp_set_object_terms( $user_id, $terms, 'competence', false);
    //clean_object_term_cache( $user_id, 'competence' );

}

}

Your Answer

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

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