Skip to content

Instantly share code, notes, and snippets.

@tripflex tripflex/functions.php
Last active Feb 19, 2019

Embed
What would you like to do?
StackOverflow WordPress User Profile Dynamic Child Taxonomy Dropdown Example
function slcustom_user_profile_fields( $user ){
$categories = smyles_get_taxonomy_hierarchy( 'project_category' );
$parent_category = $user->parent_category;
$child_category = $user->child_category;
// $parent_category = 52; // used for testing
// $child_category = 82; // used for testing
$parent_has_children = ! empty( $parent_category ) && $categories[ $parent_category ] && ! empty( $categories[ $parent_category ]->children );
// Creative way to use wp_localize_script which creates a JS variable from array
// You should actually change this to load your JavaScript file and move JS below to that file
wp_register_script( 'slcustom_user_profile_fields', '' );
wp_localize_script( 'slcustom_user_profile_fields', 'slcustom_categories', $categories );
wp_enqueue_script( 'slcustom_user_profile_fields' );
?>
<script>
jQuery( function($){
// slcustom_categories var should be available here
$('#parent_category').change( function(e){
var child_cat_select = $( '#child_category' );
var term_id = $(this).val();
console.log( 'Parent Category Changed', term_id );
// Remove any existing
child_cat_select.find( 'option' ).remove();
// Loop through children and add to children dropdown
if( slcustom_categories && slcustom_categories[ term_id ] && slcustom_categories[ term_id ]['children'] ){
$.each( slcustom_categories[term_id]['children'], function( i, v ){
child_cat_select.append( '<option value="' + v['term_id'] + '">' + v[ 'name' ] + '</option>');
});
// Show if child cats
$( '#child_category_row' ).show();
} else {
// Hide if no child cats
$( '#child_category_row' ).hide();
}
});
// Trigger change on initial page load to load child categories
$('#parent_category').change();
});
</script>
<h1 id="temppp">Select a parent taxonomy</h1>
<table class="form-table">
<tbody>
<tr>
<th>
Parent
</th>
<td>
<select name="parent_category" id="parent_category">
<?php
foreach( (array) $categories as $term_id => $cat ){
?>
<option value="<?php echo esc_attr( $term_id ) ?>"<?php echo selected( $parent_category, $term_id ); ?>><?php echo $cat->name; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr id="child_category_row" style="<?php if( ! $parent_has_children ){ echo 'display: none;'; }?>">
<th>
Child
</th>
<td>
<select name="child_category" id="child_category">
<?php
if( $parent_has_children ){
foreach( (array) $categories[$parent_category]->children as $c_term_id => $child ){
?>
<option value="<?php echo esc_attr( $c_term_id ) ?>"<?php echo selected( $child_category, $c_term_id ); ?>><?php echo $child->name; ?></option>
<?php
}
}
?>
</select>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action( 'show_user_profile', 'slcustom_user_profile_fields' );
add_action( 'edit_user_profile', 'slcustom_user_profile_fields' );
if ( ! function_exists( 'smyles_get_taxonomy_hierarchy' ) ) {
/**
* Recursively get taxonomy and its children
*
* @param string $taxonomy
* @param int $parent Parent term ID (0 for top level)
* @param array $args Array of arguments to pass to get_terms (to override default)
*
* @return array
*/
function smyles_get_taxonomy_hierarchy( $taxonomy, $parent = 0, $args = array( 'hide_empty' => false ) ) {
$defaults = array(
'parent' => $parent,
'hide_empty' => false
);
$r = wp_parse_args( $args, $defaults );
// get all direct decendants of the $parent
$terms = get_terms( $taxonomy, $r );
// prepare a new array. these are the children of $parent
// we'll ultimately copy all the $terms into this new array, but only after they
// find their own children
$children = array();
// go through all the direct decendants of $parent, and gather their children
foreach ( $terms as $term ) {
// recurse to get the direct decendants of "this" term
$term->children = smyles_get_taxonomy_hierarchy( $taxonomy, $term->term_id );
// add the term to our new array
$children[ $term->term_id ] = $term;
}
// send the results back to the caller
return $children;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.