Skip to content

Instantly share code, notes, and snippets.

@emadelawady
Last active May 14, 2019 13:44
Show Gist options
  • Save emadelawady/82c8835b29fe46698c309e0a5e3c46cd to your computer and use it in GitHub Desktop.
Save emadelawady/82c8835b29fe46698c309e0a5e3c46cd to your computer and use it in GitHub Desktop.
class for dropdown taxonomy parents & children
<?php
class dropdown_taxonomy_class {
public $tax = "adresati";
public function the_tax_hierarchy( $tax, $parent = '', $args = array( 'hide_empty' => false ) ) {
// taxonomy name or slug
$defaults = array(
'parent' => $parent,
'hide_empty' => false
);
$merge_args = wp_parse_args( $args, $defaults );
// get all direct decendants of the $parent
$terms = get_terms( $this->tax, $merge_args );
// 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 = $this->the_tax_hierarchy( $this->tax, $term->term_id );
// add the term to our new array
$children[ $term->term_id ] = $term;
}
// send the results back to the caller
return $children;
}
public function custom_select_fields() {
//This gets top layer terms only. This is done by setting parent to 0.
$categories = $this->the_tax_hierarchy( $this->tax );
$query_obj = get_queried_object();
$parent_category = $query_obj->parent_category;
$child_category = $query_obj->child_category;
$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( 'custom_select_fields', '' );
wp_localize_script( 'custom_select_fields', 'slcustom_categories', $categories );
wp_enqueue_script( 'custom_select_fields' );
?>
<h3 id="temppp">Select a parent tax</h3>
<table class="form-table">
<tbody>
<tr>
<th>
Parent
</th>
<td>
<div uk-form-custom="target: > * > span:first-child">
<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>
<button class="uk-button uk-button-default" type="button" tabindex="-1">
<span></span>
<span uk-icon="icon: pencil"></span>
</button>
</div>
</td>
</tr>
<tr id="child_category_row" style="<?php if( ! $parent_has_children ){ echo 'display: none;'; }?>">
<th>
Child
</th>
<td>
<select class="uk-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
}
}
?>
<script>
// js you can move it to your main script file
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'] ){
console.log( 'Adding Children', slcustom_categories[ term_id ]['children'] );
$.each( slcustom_categories[term_id]['children'], function( i, v ){
console.log( 'Adding Child: ', 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>
@emadelawady
Copy link
Author

emadelawady commented May 14, 2019

and then we use in taxonomy-adresati.php
<?php // the header
get_header();

// get the class of dropdown taxonomy
get_template_part( 'dropdown-tax' );

// create a new instance for the class
$custom = new dropdown_taxonomy_class();

// turn on the function of fields
$custom->custom_select_fields();

// our normal footer
get_footer(); ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment