Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. function slcustom_user_profile_fields( $user ){
  2.  
  3. $categories = smyles_get_taxonomy_hierarchy( 'project_category' );
  4. $parent_category = $user->parent_category;
  5. $child_category = $user->child_category;
  6. // $parent_category = 52; // used for testing
  7. // $child_category = 82; // used for testing
  8. $parent_has_children = ! empty( $parent_category ) && $categories[ $parent_category ] && ! empty( $categories[ $parent_category ]->children );
  9.  
  10. // Creative way to use wp_localize_script which creates a JS variable from array
  11. // You should actually change this to load your JavaScript file and move JS below to that file
  12. wp_register_script( 'slcustom_user_profile_fields', '' );
  13. wp_localize_script( 'slcustom_user_profile_fields', 'slcustom_categories', $categories );
  14. wp_enqueue_script( 'slcustom_user_profile_fields' );
  15. ?>
  16. <script>
  17. jQuery( function($){
  18. // slcustom_categories var should be available here
  19. $('#parent_category').change( function(e){
  20. var child_cat_select = $( '#child_category' );
  21. var term_id = $(this).val();
  22.  
  23. console.log( 'Parent Category Changed', term_id );
  24.  
  25. // Remove any existing
  26. child_cat_select.find( 'option' ).remove();
  27.  
  28. // Loop through children and add to children dropdown
  29. if( slcustom_categories && slcustom_categories[ term_id ] && slcustom_categories[ term_id ]['children'] ){
  30. $.each( slcustom_categories[term_id]['children'], function( i, v ){
  31. child_cat_select.append( '<option value="' + v['term_id'] + '">' + v[ 'name' ] + '</option>');
  32. });
  33.  
  34. // Show if child cats
  35. $( '#child_category_row' ).show();
  36. } else {
  37. // Hide if no child cats
  38. $( '#child_category_row' ).hide();
  39. }
  40. });
  41.  
  42. // Trigger change on initial page load to load child categories
  43. $('#parent_category').change();
  44. });
  45. </script>
  46. <h1 id="temppp">Select a parent taxonomy</h1>
  47. <table class="form-table">
  48. <tbody>
  49. <tr>
  50. <th>
  51. Parent
  52. </th>
  53. <td>
  54. <select name="parent_category" id="parent_category">
  55. <?php
  56. foreach( (array) $categories as $term_id => $cat ){
  57. ?>
  58. <option value="<?php echo esc_attr( $term_id ) ?>"<?php echo selected( $parent_category, $term_id ); ?>><?php echo $cat->name; ?></option>
  59. <?php
  60. }
  61. ?>
  62. </select>
  63. </td>
  64. </tr>
  65. <tr id="child_category_row" style="<?php if( ! $parent_has_children ){ echo 'display: none;'; }?>">
  66. <th>
  67. Child
  68. </th>
  69. <td>
  70. <select name="child_category" id="child_category">
  71. <?php
  72. if( $parent_has_children ){
  73. foreach( (array) $categories[$parent_category]->children as $c_term_id => $child ){
  74. ?>
  75. <option value="<?php echo esc_attr( $c_term_id ) ?>"<?php echo selected( $child_category, $c_term_id ); ?>><?php echo $child->name; ?></option>
  76. <?php
  77. }
  78. }
  79. ?>
  80. </select>
  81. </td>
  82. </tr>
  83. </tbody>
  84. </table>
  85. <?php
  86. }
  87.  
  88. add_action( 'show_user_profile', 'slcustom_user_profile_fields' );
  89. add_action( 'edit_user_profile', 'slcustom_user_profile_fields' );
  90.  
  91. if ( ! function_exists( 'smyles_get_taxonomy_hierarchy' ) ) {
  92. /**
  93. * Recursively get taxonomy and its children
  94. *
  95. * @param string $taxonomy
  96. * @param int $parent Parent term ID (0 for top level)
  97. * @param array $args Array of arguments to pass to get_terms (to override default)
  98. *
  99. * @return array
  100. */
  101. function smyles_get_taxonomy_hierarchy( $taxonomy, $parent = 0, $args = array( 'hide_empty' => false ) ) {
  102.  
  103. $defaults = array(
  104. 'parent' => $parent,
  105. 'hide_empty' => false
  106. );
  107. $r = wp_parse_args( $args, $defaults );
  108. // get all direct decendants of the $parent
  109. $terms = get_terms( $taxonomy, $r );
  110. // prepare a new array. these are the children of $parent
  111. // we'll ultimately copy all the $terms into this new array, but only after they
  112. // find their own children
  113. $children = array();
  114. // go through all the direct decendants of $parent, and gather their children
  115. foreach ( $terms as $term ) {
  116. // recurse to get the direct decendants of "this" term
  117. $term->children = smyles_get_taxonomy_hierarchy( $taxonomy, $term->term_id );
  118. // add the term to our new array
  119. $children[ $term->term_id ] = $term;
  120. }
  121.  
  122. // send the results back to the caller
  123. return $children;
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement