0

I created my own category selection widget. So i used <select> but i want to use with multiple attribute.

Here is my widget class. It works perfect without multiple attr. But i want to use it. And it must be return category ids in array.

<?php

class myCatWidget extends WP_Widget {

    function myCatWidget() {
        parent::WP_Widget( false, $name = 'My cat widget' );
    }

    function form( $instance ) {
        $title = esc_attr( $instance[ 'title' ] );
        $postCats = $instance[ 'postCats' ];
        ?>

        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
            <input type="text" class="widfat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" style="width: 100%;" value="<?php echo $title; ?>"/>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'postCats' ); ?>">Categories</label>
            <select name="<?php echo $this->get_field_name( 'postCats' ); ?>" id="<?php echo $this->get_field_id( 'postCats' ); ?>" style="width: 100%;" multiple>
                <?php
                $args = array(
                    'taxonomy' => 'category',
                );

                $terms = get_terms( $args );
                foreach( $terms as $term ) { ?>
                    <option <?php selected( $instance[ 'postCats' ], $term->term_id ); ?> value="<?php echo esc_attr( $term->term_id ); ?>">
                        <?php echo esc_html( $term->name ); ?>
                    </option>
                <?php } ?>
            </select>
        </p>

        <?php

    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
        $instance[ 'postCats' ] = esc_sql( $new_instance[ 'postCats' ] );
        return $instance;
    }

    function widget( $args, $instance ) {
        extract( $args );

        $title = apply_filters( 'widget_title', $instance[ 'title' ] );
        $postCats = $instance[ 'postCats' ];

        echo $before_widget;

        if( $title ) {
            echo $before_title . $title . $after_title;
        }

        echo $postCats;

        echo $after_widget;
    }
}

add_action( 'widgets_init', create_function( '', 'return register_widget("myCatWidget");' ) );

?>
1

Hi why don't you used multiple check-box instead of select option field. As well as you have used deprecated functions like "create_function". I have modified your code which replaced select option with multiple check-box selection.

function myCatWidget() {
    register_widget( 'my_custom_cat_widget' );
}
add_action( 'widgets_init', 'myCatWidget' );

class my_custom_cat_widget extends WP_Widget {

   function __construct() { 
        parent::__construct('my_custom_cat_widget', __('My cat widget', 'wpb_widget_domain'), 
            array( 'description' => __( 'Your description', 'wpb_widget_domain' ), ) 
        );
    }

public function form( $instance ) {
    $title = isset($instance[ 'title' ]) ? $instance[ 'title' ] : 'Categories';
    $instance['postCats'] = !empty($instance['postCats']) ? explode(",",$instance['postCats']) : array();
    ?>

    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
        <input type="text" class="widfat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" style="width: 100%;" value="<?php echo $title; ?>"/>
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'postCats' ); ?>"><?php _e( 'Select Categories you want to show:' ); ?></label><br />
        <?php $args = array(
                'post_type' => 'post',
                'taxonomy' => 'category',
            );
            $terms = get_terms( $args );
        //print_r($terms);
         foreach( $terms as $id => $name ) { 
            $checked = "";
            if(in_array($name->name,$instance['postCats'])){
                $checked = "checked='checked'";
            }
        ?>
            <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('postCats'); ?>" name="<?php echo $this->get_field_name('postCats[]'); ?>" value="<?php echo $name->name; ?>"  <?php echo $checked; ?>/>
            <label for="<?php echo $this->get_field_id('postCats'); ?>"><?php echo $name->name; ?></label><br />
        <?php } ?>
    </p>

    <?php

}

public function update( $new_instance, $old_instance ) {
    $instance = $old_instance;

    $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
    $instance['postCats'] = !empty($new_instance['postCats']) ? implode(",",$new_instance['postCats']) : 0;
    return $instance;
}

public function widget( $args, $instance ) {
    extract( $args );

    $title = apply_filters( 'widget_title', $instance[ 'title' ] );
    $postCats = $instance[ 'postCats' ];
    $categories_list = explode(",", $postCats);

    echo $before_widget;

    if( $title ) {
        echo $before_title . $title . $after_title;
    }

    $args = array('post_type' => 'post','taxonomy' => 'category',);
    $terms = get_terms( $args );
    ?>
    <ul>
        <?php
            foreach ($categories_list as $cat) {
                foreach($terms as $term) {
                    if($cat === $term->name) {
                        echo "<li>".$term->name."</li>";                                    
                    }
                }
            }
        ?>
    </ul>
    <?php
    echo $after_widget;
}
}

Hope it will help to solved your problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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