Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*** code à copier dans le functions.php du thème actif à partir d'ici ***/
- function chritchan_get_select_cat( $selected_id = false ) {
- $args = array(
- 'orderby' => 'name', /* ou id ou count .. */
- 'order' => 'ASC', /* ou DESC */
- 'hide_empty' => 0 /* ou 1 si on ne veut que des catégories ayant des articles rattachés */
- );
- // si custom post type utiliser l'identifiant de la custom taxo au lieu de category
- $cats = get_terms( 'category', $args );
- $select = '<select name="_dept_chritchan"><option value="0">Choisir</option>';
- foreach( $cats as $term ) {
- $selected = false;
- if( $selected_id == $term->term_id )
- $selected = 'selected';
- $select .= '<option value="'.$term->term_id.'" '.$selected.' >'.$term->name.'</option>';
- }
- $select .= '</select>';
- // si on veut modifier par un filtre le select box
- return apply_filters('chritchan_get_select_cat', $select, $cats );
- }
- function edit_chritchan_select_cat() {
- global $bp;
- $user_id = $bp->displayed_user->id;
- $selected_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
- if( bp_is_user_profile_edit() ) {
- ?>
- <label for="_dept_chritchan">Votre département</label>
- <?php echo chritchan_get_select_cat( $selected_id );?>
- <?php
- } else {
- $term_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
- if( empty( $term_id ) )
- return false;
- // changer category pour l'identifiant de la custom taxo
- $cat = get_term_by( 'id', $term_id, 'category' );
- ?>
- <table class="profile-fields">
- <tr<?php bp_field_css_class(); ?>>
- <td class="label">Les articles de votre département</td>
- <td class="data"><a href="<?php echo get_term_link( $cat, 'category' );?>"><?php echo $cat->name?></a></td>
- </tr>
- </table>
- <?php
- }
- }
- add_action( 'bp_after_profile_field_content', 'edit_chritchan_select_cat');
- function save_chritchan_select_cat( $user_id, $posted_field_ids, $errors) {
- if( !empty( $_POST['_dept_chritchan'] ) )
- update_user_meta($user_id, 'chritchan_dept', intval( $_POST['_dept_chritchan'] ) );
- if( $_POST['_dept_chritchan'] == 0 )
- delete_user_meta( $user_id, 'chritchan_dept' );
- }
- add_action('xprofile_updated_profile', 'save_chritchan_select_cat', 10, 3);
- /*** fin de la copie ***/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement