Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    }
share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

I got it :-) thanks to this

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['kwtax'] = strip_tags( $new_instance['kwtax'] );

        return $instance;
    }

    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( 'title' => __('Equipment Search', 'agriquip'), 'kwtax' => '');
        $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>

          <p>
            <select id="<?php echo $this->get_field_id('kwtax'); ?>" name="<?php echo $this->get_field_name('kwtax'); ?>" class="widefat" style="width:100%;">
                <?php foreach(get_terms('exc_equipment_cat','parent=0&hide_empty=0') as $term) { ?>
                <option <?php selected( $instance['kwtax'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
                <?php } ?>      
            </select>
        </p>
share|improve this answer
add comment

Thank you HaneD.

Your example is probably the only example that I could find that works. If someone else wants to get it to work without making a custom category try to replace:

<?php foreach(get_terms('exc_equipment_cat','parent=0&hide_empty=0') as $term) { ?>

with

<?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
share|improve this answer
add comment

You were so close, all you had to do was find your category ID from the update method. Like so:

    $instance['exc_equipment_cat'] = filter_var($new_instance['exc_equipment_cat'], FILTER_SANITIZE_NUMBER_INT);

Or however you want to sanitize the number coming from the update.

And that will give you the category or taxonomy id.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.