Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Custom Taxonomy drop downs for parents and child
<?php
/*
Plugin Name: Custom Taxonomy DropDown
Author: Hameedullah Khan
Aurhot URI: http://hameedullah.com
*/
// change this to your taxonomy
$brand_taxonomy = 'category';
$taxonomy_name = 'Brands';
/* registering taxonomy */
/* disabled assuming you already have regsitered taxonomy */
/*
add_action('init', 'my_taxonomy');
function my_taxonomy() {
global $brand_taxonomy, $taxonomy_name;
register_taxonomy($brand_taxonomy,array('post'),array('labels'=>array('name'=>$taxonomy_name),'public'=>true,'hierarchical'=>true));
}*/
add_action('add_meta_boxes', 'my_custom_metabox');
function my_custom_metabox() {
add_meta_box('custom-taxonomy-dropdown','Brands','taxonomy_dropdowns_box','post','side','high');
}
function taxonomy_dropdowns_box( $post ) {
global $brand_taxonomy, $taxonomy_name;
wp_nonce_field('custom-dropdown', 'dropdown-nonce');
$terms = get_terms( $brand_taxonomy, 'hide_empty=0');
if ( is_a( $terms, 'WP_Error' ) ) {
$terms = array();
}
$object_terms = wp_get_object_terms( $post->ID, $brand_taxonomy, array('fields'=>'ids'));
if ( is_a( $object_terms, 'WP_Error' ) ) {
$object_terms = array();
}
// you can move the below java script to admin_head
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#custombrandoptions').change(function() {
var custombrand = jQuery('#custombrandoptions').val();
if ( custombrand == '0') {
jQuery('#custommodeloptions').html('');
jQuery('#modelcontainer').css('display', 'none');
} else {
jQuery('#ctd-custom-taxonomy-terms-loading').css('display', 'inline');
jQuery('#modelcontainer').css('display', 'none');
var data = {
'action':'get_brand_models',
'custombrand':custombrand,
'dropdown-nonce': jQuery('#dropdown-nonce').val()
};
jQuery.post(ajaxurl, data, function(response){
jQuery('#custommodeloptions').html(response);
jQuery('#ctd-custom-taxonomy-terms-loading').css('display', 'none');
jQuery('#modelcontainer').css('display', 'inline');
});
}
});
});
</script>
<?php
echo "Brand:";
echo "<select id='custombrandoptions' name='custombrands[]'>";
echo "<option value='0'>None</option>";
foreach ( $terms as $term ) {
if ( $term->parent == 0) {
if ( in_array($term->term_id, $object_terms) ) {
$parent_id = $term->term_id;
echo "<option value='{$term->term_id}' selected='selected'>{$term->name}</option>";
} else {
echo "<option value='{$term->term_id}'>{$term->name}</option>";
}
}
}
echo "</select><br />";
echo "<div id='ctd-custom-taxonomy-terms-loading' style='display:none;'>Loading...</div>";
echo "<div id='modelcontainer'";
if ( !isset( $parent_id)) echo " style='display: none;'";
echo ">";
echo "Models:";
echo "<select id='custommodeloptions' name='custombrands[]'>";
if ( isset( $parent_id)) {
$models = get_terms( $brand_taxonomy, 'hide_empty=0&child_of='.$parent_id);
foreach ( $models as $model ) {
if ( in_array($model->term_id, $object_terms) ) {
echo "<option value='{$model->term_id}' selected='selected'>{$model->name}</option>";
} else {
echo "<option value='{$model->term_id}'>{$model->name}</option>";
}
}
}
echo "</select>";
echo "</div>";
}
add_action('save_post','save_my_custom_taxonomy');
function save_my_custom_taxonomy( $post_id ) {
global $brand_taxonomy, $taxonomy_name;
if ( define('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce($_POST['dropdown-nonce'], 'custom-dropdown'))
return;
$brands = array_map('intval', $_POST['custombrands']);
wp_set_object_terms($post_id, $brands, $brand_taxonomy);
}
add_action('wp_ajax_get_brand_models', 'get_brand_models');
function get_brand_models() {
global $brand_taxonomy, $taxonomy_name;
check_ajax_referer('custom-dropdown', 'dropdown-nonce');
if (isset($_POST['custombrand'])) {
$models = get_terms( $brand_taxonomy, 'hide_empty=0&child_of='. $_POST['custombrand']);
echo "<option value='0'>Select one</option>";
foreach ($models as $model) {
echo "<option value='{$model->term_id}'>{$model->name}</option>";
}
}
die();
}
?>
@mamouneyya

This comment has been minimized.

Show comment Hide comment
@mamouneyya

mamouneyya Jun 26, 2011

Thank you so much, your code works like a charm! I even could to modify it to work with three levels taxonomy tree (3 drop-downs). I just miss a simple loading indicator beside the field when retrieving the drop-down values, which improves the user experience. Could that be easily added? I would appreciate if you guided me.

Thank you so much, your code works like a charm! I even could to modify it to work with three levels taxonomy tree (3 drop-downs). I just miss a simple loading indicator beside the field when retrieving the drop-down values, which improves the user experience. Could that be easily added? I would appreciate if you guided me.

@hameedullah

This comment has been minimized.

Show comment Hide comment
@hameedullah

hameedullah Jun 27, 2011

Good Idea, I will try to add that tonight.

Owner

hameedullah commented Jun 27, 2011

Good Idea, I will try to add that tonight.

@mamouneyya

This comment has been minimized.

Show comment Hide comment
@mamouneyya

mamouneyya Jul 2, 2011

Not to be annoying, but did you have any chance to look at it?

Not to be annoying, but did you have any chance to look at it?

@hameedullah

This comment has been minimized.

Show comment Hide comment
@hameedullah

hameedullah Jul 3, 2011

Sorry.. wasn't feeling well so couldn't add the loading indicator earlier. But just added the loading indicator to it, you can check.

Owner

hameedullah commented Jul 3, 2011

Sorry.. wasn't feeling well so couldn't add the loading indicator earlier. But just added the loading indicator to it, you can check.

@mamouneyya

This comment has been minimized.

Show comment Hide comment
@mamouneyya

mamouneyya Jul 3, 2011

Works great. Thanks hameedullah :)

Works great. Thanks hameedullah :)

@foxsk8

This comment has been minimized.

Show comment Hide comment
@foxsk8

foxsk8 Oct 25, 2011

Here is full moded version of this plugin. Mod make wp_drop_down categories with sublevels and paddings. Mod fix update and save functions.

Here is full moded functions code.

http://paste.php.lv/dc8786ed7b99038807c7f9741b30794a?lang=php

For output and custom query use: ID, 'cat_include', true) ?>

foxsk8 commented Oct 25, 2011

Here is full moded version of this plugin. Mod make wp_drop_down categories with sublevels and paddings. Mod fix update and save functions.

Here is full moded functions code.

http://paste.php.lv/dc8786ed7b99038807c7f9741b30794a?lang=php

For output and custom query use: ID, 'cat_include', true) ?>

@mamouneyya

This comment has been minimized.

Show comment Hide comment
@mamouneyya

mamouneyya Oct 26, 2011

@foxsk8 Looks like non-PHP parts (JS and HTML) got dropped from your code?

@foxsk8 Looks like non-PHP parts (JS and HTML) got dropped from your code?

@foxsk8

This comment has been minimized.

Show comment Hide comment
@foxsk8

foxsk8 Oct 26, 2011

js and html has droped, and drop fuction has change on standart wordpress core drop downs and update save, update fuctions. This mod also put meta box in database without serialize data.

foxsk8 commented Oct 26, 2011

js and html has droped, and drop fuction has change on standart wordpress core drop downs and update save, update fuctions. This mod also put meta box in database without serialize data.

@nackle2k10

This comment has been minimized.

Show comment Hide comment
@nackle2k10

nackle2k10 Dec 15, 2011

How to use this code in the front end?

How to use this code in the front end?

@Thunderlab

This comment has been minimized.

Show comment Hide comment
@Thunderlab

Thunderlab Feb 15, 2012

Can you show how to make it working with 3 dropdowns? (3 taxonomy levels)

Can you show how to make it working with 3 dropdowns? (3 taxonomy levels)

@unpapelito

This comment has been minimized.

Show comment Hide comment
@unpapelito

unpapelito Mar 19, 2012

Hello! It is i need! Thanks! But do you know how i can add a third level? 3 dropdowns?

Hello! It is i need! Thanks! But do you know how i can add a third level? 3 dropdowns?

@albertrubis17

This comment has been minimized.

Show comment Hide comment
@albertrubis17

albertrubis17 Sep 18, 2012

Hi hameedullah, is this the code of a drop-down list that get only checked taxonomy of a post in the wp-admin and displayed by query in a page(created by category).

Hi hameedullah, is this the code of a drop-down list that get only checked taxonomy of a post in the wp-admin and displayed by query in a page(created by category).

@palcas

This comment has been minimized.

Show comment Hide comment
@palcas

palcas Mar 7, 2013

mamouneyya could you show how you managed to work this with three levels taxonomy?

palcas commented Mar 7, 2013

mamouneyya could you show how you managed to work this with three levels taxonomy?

@jeffersonrbr

This comment has been minimized.

Show comment Hide comment
@jeffersonrbr

jeffersonrbr Sep 26, 2013

you really really helped me!!!
tks a lot man!!!!

you really really helped me!!!
tks a lot man!!!!

@ricklambrechts

This comment has been minimized.

Show comment Hide comment
@ricklambrechts

ricklambrechts Feb 1, 2017

Thank you!
This is still really useful!

Thank you!
This is still really useful!

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