Advertisement
Guest User

Add Taxonomy to WP Users

a guest
May 14th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. define( 'USER_CATEGORY_NAME', 'user_category' );
  2. define( 'USER_CATEGORY_META_KEY', '_user_category' );
  3.  
  4. add_action( 'init', 'ms_register_user_department_taxonomy' );
  5.  
  6. function ms_register_user_department_taxonomy() {
  7. register_taxonomy(
  8. USER_CATEGORY_NAME, //taxonomy name
  9. 'user', //object for which the taxonomy is created
  10. array( //taxonomy details
  11. 'public' => true,
  12. 'labels' => array(
  13. 'name' => 'User Departments',
  14. 'singular_name' => 'User Department',
  15. 'menu_name' => 'User Departments',
  16. 'search_items' => 'Search User Department',
  17. 'popular_items' => 'Popular User Departments',
  18. 'all_items' => 'All User Departments',
  19. 'edit_item' => 'Edit User Department',
  20. 'update_item' => 'Update User Department',
  21. 'add_new_item' => 'Add New User Department',
  22. 'new_item_name' => 'New User Department Name',
  23. ),
  24. 'update_count_callback' => function() {
  25. return; //important
  26. }
  27. )
  28. );
  29. }
  30.  
  31. add_action( 'admin_menu', 'ms_add_user_departments_admin_page' );
  32.  
  33. function ms_add_user_departments_admin_page() {
  34. $taxonomy = get_taxonomy( USER_CATEGORY_NAME );
  35. add_users_page(
  36. esc_attr( $taxonomy->labels->menu_name ),//page title
  37. esc_attr( $taxonomy->labels->menu_name ),//menu title
  38. $taxonomy->cap->manage_terms,//capability
  39. 'edit-tags.php?taxonomy=' . $taxonomy->name//menu slug
  40. );
  41. }
  42.  
  43. add_filter( 'submenu_file', 'ms_set_user_department_submenu_active' );
  44.  
  45. function ms_set_user_department_submenu_active( $submenu_file ) {
  46. global $parent_file;
  47. if( 'edit-tags.php?taxonomy=' . USER_CATEGORY_NAME == $submenu_file ) {
  48. $parent_file = 'users.php';
  49. }
  50. return $submenu_file;
  51. }
  52.  
  53. add_action( 'show_user_profile', 'ms_admin_user_profile_department_select' );
  54. add_action( 'edit_user_profile', 'ms_admin_user_profile_department_select' );
  55.  
  56. function ms_admin_user_profile_department_select( $user ) {
  57. $taxonomy = get_taxonomy( USER_CATEGORY_NAME );
  58.  
  59. if ( !user_can( $user, 'employee' ) ) {
  60. return;
  61. }
  62. ?>
  63. <table class="form-table">
  64. <tr>
  65. <th>
  66. <label for="<?php echo USER_CATEGORY_META_KEY ?>">User Department</label>
  67. </th>
  68. <td>
  69. <?php
  70. $user_category_terms = get_terms( array(
  71. 'taxonomy' => USER_CATEGORY_NAME,
  72. 'hide_empty' => 0
  73. ) );
  74.  
  75. $select_options = array();
  76.  
  77. foreach ( $user_category_terms as $term ) {
  78. $select_options[$term->term_id] = $term->name;
  79. }
  80.  
  81. $meta_values = get_user_meta( $user->ID, USER_CATEGORY_META_KEY, true );
  82.  
  83. echo ms_custom_form_select(
  84. USER_CATEGORY_META_KEY,
  85. $meta_values,
  86. $select_options,
  87. '',
  88. array( 'multiple' =>'multiple' )
  89. );
  90. ?>
  91. </td>
  92. </tr>
  93. </table>
  94. <?php
  95. }
  96.  
  97.  
  98. function ms_custom_form_select( $name, $value, $options, $default_var ='', $html_params = array() ) {
  99. if( empty( $options ) ) {
  100. $options = array( '' => '---choose---');
  101. }
  102.  
  103. $html_params_string = '';
  104.  
  105. if( !empty( $html_params ) ) {
  106. if ( array_key_exists( 'multiple', $html_params ) ) {
  107. $name .= '[]';
  108. }
  109. foreach( $html_params as $html_params_key => $html_params_value ) {
  110. $html_params_string .= " {$html_params_key}='{$html_params_value}'";
  111. }
  112. }
  113.  
  114. echo "<select name='{$name}'{$html_params_string}>";
  115.  
  116. foreach( $options as $options_value => $options_label ) {
  117. if( ( is_array( $value ) && in_array( $options_value, $value ) )
  118. || $options_value == $value ) {
  119. $selected = " selected='selected'";
  120. } else {
  121. $selected = '';
  122. }
  123. if( empty( $value ) && !empty( $default_var ) && $options_value == $default_var ) {
  124. $selected = " selected='selected'";
  125. }
  126. echo "<option value='{$options_value}'{$selected}>{$options_label}</option>";
  127. }
  128.  
  129. echo "</select>";
  130. }
  131.  
  132.  
  133. add_action( 'personal_options_update', 'ms_admin_save_user_departments' );
  134. add_action( 'edit_user_profile_update', 'ms_admin_save_user_departments' );
  135.  
  136. function ms_admin_save_user_departments( $user_id ) {
  137. $tax = get_taxonomy( USER_CATEGORY_NAME );
  138. $user = get_userdata( $user_id );
  139.  
  140. if ( !user_can( $user, 'employee' ) ) {
  141. return false;
  142. }
  143.  
  144. $new_categories_ids = $_POST[USER_CATEGORY_META_KEY];
  145. $user_meta = get_user_meta( $user_id, USER_CATEGORY_META_KEY, true );
  146. $previous_categories_ids = array();
  147.  
  148. if( !empty( $user_meta ) ) {
  149. $previous_categories_ids = (array)$user_meta;
  150. }
  151.  
  152. if( ( current_user_can( 'administrator' ) && $_POST['role'] != 'employee' ) ) {
  153. delete_user_meta( $user_id, USER_CATEGORY_META_KEY );
  154. ms_update_users_departments_count( $previous_categories_ids, array() );
  155. } else {
  156. update_user_meta( $user_id, USER_CATEGORY_META_KEY, $new_categories_ids );
  157. ms_update_users_departments_count( $previous_categories_ids, $new_categories_ids );
  158. }
  159. }
  160.  
  161.  
  162. function ms_update_users_department_count( $previous_terms_ids, $new_terms_ids ) {
  163. global $wpdb;
  164.  
  165. $terms_ids = array_unique( array_merge( (array)$previous_terms_ids, (array)$new_terms_ids ) );
  166.  
  167. if( count( $terms_ids ) < 1 ) { return; }
  168.  
  169. foreach ( $terms_ids as $term_id ) {
  170. $count = $wpdb->get_var(
  171. $wpdb->prepare(
  172. "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value LIKE %s",
  173. USER_CATEGORY_META_KEY,
  174. '%"' . $term_id . '"%'
  175. )
  176. );
  177. $wpdb->update( $wpdb->term_taxonomy, array( 'count' => $count ), array( 'term_taxonomy_id' => $term_id ) );
  178. }
  179. }
  180.  
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement