Advertisement
imath

Attacher une catégorie à un profil utilisateur

Nov 23rd, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?php
  2. /*** code à copier dans le functions.php du thème actif à partir d'ici ***/
  3.  
  4. function chritchan_get_select_cat( $selected_id = false ) {
  5.  
  6.     $args = array(
  7.             'orderby'    => 'name', /* ou id ou count .. */
  8.             'order'      => 'ASC', /* ou DESC */
  9.             'hide_empty' => 0 /* ou 1 si on ne veut que des catégories ayant des articles rattachés */
  10.         );
  11.        
  12.     // si custom post type utiliser l'identifiant de la custom taxo au lieu de category
  13.     $cats = get_terms( 'category', $args );
  14.    
  15.     $select = '<select name="_dept_chritchan"><option value="0">Choisir</option>';
  16.    
  17.     foreach( $cats as $term ) {
  18.         $selected = false;
  19.        
  20.         if( $selected_id == $term->term_id )
  21.             $selected = 'selected';
  22.        
  23.         $select .= '<option value="'.$term->term_id.'" '.$selected.' >'.$term->name.'</option>';
  24.     }
  25.    
  26.     $select .= '</select>';
  27.    
  28.     // si on veut modifier par un filtre le select box
  29.     return apply_filters('chritchan_get_select_cat', $select, $cats );
  30. }
  31.  
  32. function edit_chritchan_select_cat() {
  33.     global $bp;
  34.    
  35.     $user_id = $bp->displayed_user->id;
  36.    
  37.     $selected_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
  38.    
  39.     if( bp_is_user_profile_edit() ) {
  40.         ?>
  41.        
  42.         <label for="_dept_chritchan">Votre département</label>
  43.         <?php echo chritchan_get_select_cat( $selected_id );?>
  44.         <?php
  45.        
  46.     } else {
  47.        
  48.         $term_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
  49.        
  50.         if( empty( $term_id ) )
  51.             return false;
  52.  
  53.         // changer category pour l'identifiant de la custom taxo
  54.         $cat = get_term_by( 'id', $term_id, 'category' );
  55.  
  56.         ?>
  57.         <table class="profile-fields">
  58.            
  59.             <tr<?php bp_field_css_class(); ?>>
  60.  
  61.                 <td class="label">Les articles de votre département</td>
  62.  
  63.                 <td class="data"><a href="<?php echo get_term_link( $cat, 'category' );?>"><?php echo $cat->name?></a></td>
  64.  
  65.             </tr>
  66.         </table>
  67.         <?php
  68.     }
  69.    
  70. }
  71.  
  72. add_action( 'bp_after_profile_field_content', 'edit_chritchan_select_cat');
  73.  
  74. function save_chritchan_select_cat( $user_id, $posted_field_ids, $errors) {
  75.     if( !empty( $_POST['_dept_chritchan'] ) )
  76.         update_user_meta($user_id, 'chritchan_dept', intval( $_POST['_dept_chritchan'] ) );
  77.        
  78.     if( $_POST['_dept_chritchan'] == 0 )
  79.         delete_user_meta( $user_id, 'chritchan_dept' );
  80. }
  81.  
  82. add_action('xprofile_updated_profile', 'save_chritchan_select_cat', 10, 3);
  83.  
  84. /*** fin de la copie ***/
  85.  
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement