sandi009
Forum Replies Created
-
Forum: Plugins
In reply to: [aCategory Dropdown List] Frontend FormHi moehrenstein,
I have just updated existing plugin code with following code:
paste this code on page where you want to use. I have done it for product category, You can change code as per your requirements.
function aCatSelect($post, $catSlug){
global $post;
$arr = array(‘slug’=>’product_cat’,
‘replace’=>0,
‘none’=>1,
‘orderby’=>’name’,
‘order’=>’ASC’,
‘multi’=>1);$obj = new stdClass();
foreach ($arr as $key => $value){
$obj->$key = $value;
}$options = $obj;
$catSlug = ‘product_cat’;//$thisPostType = $post->post_type;
if(is_admin()){
$argName = ($options->slug == “category”) ? ‘post_category[]’ : ‘tax_input[‘.$catSlug.’][]’;
}else{
$argName = ($options->slug == “category”) ? ‘post_category[]’ : ‘product_cat[]’;
}
$orderby = $options->orderby;
$order = $options->order;$depth = ($options->multi) ? 1 : 1; //$options->depth; // 0 – show all levels
$none = ($options->none == 1 && $options->slug != “category”) ? __(‘None’) : 0;
$default_category = get_option(‘default_category’);
$post_categories = wp_get_post_terms( $post->ID, $catSlug );
if (!$post_categories) $post_categories = array(0);echo ‘<div>’;
$i = 0;
$child_of = 0;
$selected = null;
while( $post_categories[0] && $i < count($post_categories) ){
$c = $post_categories[$i];
if($c->parent == $child_of){
$selected = ($c->term_id) ? $c->term_id : $default_category;if($child_of > 0) $none = __(‘None’);
$args = array(
‘orderby’ => $orderby,
‘order’ => $order,
‘show_option_none’ => $none,
‘hide_empty’ => 0,
‘echo’ => 1,
‘selected’ => $selected,
‘hierarchical’ => 1,
‘name’ => $argName,
‘class’ => ‘select-pad dokan-form-control postform acategory ‘.$catSlug,
‘depth’ => $depth,
‘tab_index’ => 0,
‘child_of’ => $child_of,
‘taxonomy’ => $catSlug,
‘hide_if_empty’ => false );
wp_dropdown_categories( $args );$child_of = $selected;
$i=0;
}
else $i++;
}$lastcatargs = array(
‘child_of’ => $child_of,
‘hide_empty’ => 0,
‘hierarchical’ => 1,
‘taxonomy’ => $catSlug,
‘pad_counts’ => false );
$lastcat = get_categories( $lastcatargs );if($lastcat){
$args = array(
‘orderby’ => $orderby,
‘order’ => $order,
‘show_option_none’ => __(‘None’),
‘hide_empty’ => 0,
‘echo’ => 1,
‘selected’ => $selected,
‘hierarchical’ => 1,
‘name’ => $argName,
‘class’ => ‘select-pad dokan-form-control postform acategory ‘.$catSlug,
‘depth’ => $depth,
‘tab_index’ => 0,
‘child_of’ => $child_of,
‘taxonomy’ => $catSlug,
‘hide_if_empty’ => false );
wp_dropdown_categories( $args );
}echo ‘</div><span class=”spinner acat_load” style=”position:absolute; bottom:5px; right:38px;”></span>’;
?><style type=”text/css”>select.acategory{width:250px;}</style>
<script type=”application/javascript”>
jQuery(function($) {
$(‘body’).on(‘change’, ‘select.acategory.<?php echo $catSlug;?>’, function(){
var $this = $(this),
level = $this.index(),
maxlevel = $(‘select.acategory.<?php echo $catSlug;?>’).length;for(i = level+1 ; i < maxlevel; i++){
$(‘select.acategory:eq(‘+i+’)’).addClass(‘remove’);
}$(‘select.acategory.remove’).remove()
<?php if($options->multi){?>
$(‘#a-<?php echo $catSlug;?> .acat_load’).show();var data = {
action: “aCatGetChildren”,
parent: $(this).val(),
catSlug: ‘<?php echo $catSlug;?>’,
orderby: ‘<?php echo $options->orderby;?>’,
order: ‘<?php echo $options->order;?>’,
argName: ‘<?php echo $argName;?>’,
};
$.post(ajaxurl, data, function(response) {
$this.parent().append(response);
$(‘#a-<?php echo $catSlug;?> .acat_load’).hide();
});
<?php
}
?>
});
});
</script>
<?php
}add_action(‘wp_ajax_aCatGetChildren’, ‘aCatGetChildren’);
function aCatGetChildren(){
global $wpdb, $post;
$parent = $_POST[‘parent’];
$catSlug = $_POST[‘catSlug’];
$orderby = $_POST[‘orderby’];
$order = $_POST[‘order’];
$argName = $_POST[‘argName’];$acat_children = $wpdb->get_results(” SELECT * FROM $wpdb->term_taxonomy WHERE parent = $parent”);
$children = ($acat_children ? true : exit());$args = array(
‘orderby’ => $orderby,
‘order’ => $order,
‘show_option_none’ => __(‘None’),
‘hide_empty’ => 0,
‘echo’ => 1,
‘hierarchical’ => 1,
‘name’ => $argName,
‘class’ => ‘select-pad dokan-form-control postform acategory ‘.$catSlug,
‘depth’ => 1,
‘tab_index’ => 0,
‘child_of’ => $parent,
‘taxonomy’ => $catSlug,
‘hide_if_empty’ => false );wp_dropdown_categories( $args );
exit();
}Thanks,
Sandy