WordPress Update taxonomy checkbox user terms

Question

Hello I created a taxonomy in the user part (‘user_activities’). I would just like to be able to save the changes via the user’s frontend profile. Here is my code:

PHP TERMS:

<?php $categories = get_terms('user_activities', array('hide_empty' => 0));
if ( !empty( $categories ) ) {
        foreach ( $categories as $categorie ) { 
        ?>
          <label for="user_activities-<?php echo esc_attr( $categorie->slug ); ?>">
            <input type="checkbox" name="user_activities[]" id="user_activities-<?php echo esc_attr( $categorie->slug ); ?>" value="<?php echo $categorie->slug; ?>" <?php if ( $pagenow !== 'user-new.php' ) checked( true, is_object_in_term( $userID, 'user_activities', $categorie->slug ) ); ?>>
            <?php echo $categorie->name; ?>
          </label><br/>
        <?php
        }
      }?>

HTML VIEW:

<div class="multiselect">

<div id="user_activities_form">
           <label for="user_activities-randonnee">
        <input type="checkbox" name="user_activities[]" id="user_activities-randonnee" value="randonnee">
        RANDONNEE          </label><br>
              <label for="user_activities-vtt">
        <input type="checkbox" name="user_activities[]" id="user_activities-vtt" value="vtt">
        VTT          </label><br>
          
</div>

UPDATE FUNCTION:

     $current_user   =   wp_get_current_user();
        $userID         =   $current_user->ID;
        $useractivities  =  wp_kses($_POST['user_activities'],$allowed_html);

     
foreach($useractivities as $useractivitie){
    if(isset($_POST['user_activities-'.$useractivitie->slug]))
    wp_set_object_terms($userID,$useractivitie->slug,'user_activities',true); 
    else
    wp_set_object_terms($userID,$useractivitie->slug,'user_activities',false);      
}

Nothing updates, what errors did I make in the update function?
Thanks for your help !

0
Anarko 4 months 0 Answers 9 views 0

Leave an answer