I have a product search widget when you select certain products a hidden field is shown. I would like to set a custom category in my widget options that is then used for displaying my hidden field.
I have the front end working 100% when I add the parent category_id manually to my code. Now I just need to set the category option in my back end.
This is what I have currently but its not working in the sense that its not storing the selection that is made. I left out the search form because it doens't use any of the widget options.
class Equipment_Search extends WP_Widget {
function Equipment_Search() {
$widget_ops = array( 'classname' => 'agriquip', 'description' => __('Displays the Equipment Search Form', 'agriquip') );
$control_ops = array( 'width' => 200, 'height' => 350, 'id_base' => 'agriquip-widget' );
$this->WP_Widget( 'agriquip-widget', __('Equipment Search', 'agriquip'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$tract_id = isset( $instance['exc_equipment_cat'] ) ? $instance['exc_equipment_cat'] : false;
$tract = wp_list_pluck(get_terms('exc_equipment_cat', array('parent' => 3)), 'slug');
$tractparent = get_term_by('id','3', 'exc_equipment_cat');
$tractparent = $tractparent->slug;
echo $before_widget;
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['exc_equipment_cat'] = $new_instance['exc_equipment_cat'] ? 1 : 0;
return $instance;
}
function form( $instance ) {
//Set up some default widget settings.
$defaults = array( 'title' => __('Equipment Search', 'agriquip'), 'exc_equipment_cat' => '-1');
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'agriquip'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" />
</p>
<form method="post" action="">
<label for="exc_equipment_cat">Category to use with Kw Options</label>
<?php
$dropdown_args = array(
'taxonomy' => 'exc_equipment_cat',
'name' => $this->get_field_name('exc_equipment_cat'),
'show_count' => 1,
'orderby' => 'name',
'hierarchical' => true,
'echo' => 0,
'depth' => 1,
'show_option_none' => 'Select Category',
'selected' => (int)$instance['exc_equipment_cat'],
);
wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $dropdown_args));
$select = wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $dropdown_args));
$select = preg_replace("#<select([^>]*)>#", "<select$1 class='widefat'>", $select);
echo $select;
?>
</form>
<?php
}