Example:
<?php class Category_Fields_Widget extends WP_Widget { function __construct() { // Instantiate the parent object $widget_ops = array( 'classname' => 'widget_title_fields_entries', 'description' => esc_attr__('Category Field Widget - Created by deshisoft', 'eye-theme') ); $control_ops = array( 'width' => 275 ); parent::__construct( false, 'Category Field Widget', $widget_ops, $control_ops ); } function widget( $args, $instance ) { extract( $args ); // Widget output $title = apply_filters('widget_title', $instance['title']); $category = apply_filters('widget_category', $instance['category']); $category_d = apply_filters('widget_category_d', $instance['category_d']); echo $before_widget; echo '<div style="border:1px solid #ddd; background:#f6f6f6;padding:15px;">'; // Category wp_list_categories($category); echo 'Category:' . $instance['category'] . $category .'<br/>'; // Drop Down Category List $args = array( 'name' => $category, 'show_option_none' => __( 'Select category' ), 'show_count' => 1, 'orderby' => 'name', 'echo' => 0, 'selected' => $category, 'class' => 'widefat', 'id' => 'cat' ); echo wp_dropdown_categories($args); ?> <script type='text/javascript'> /* <![CDATA[ */ var dropdown = document.getElementById("cat"); function onCatChange() { if ( dropdown.options[dropdown.selectedIndex].value > 0 ) { location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value; } } dropdown.onchange = onCatChange; /* ]]> */ </script> <?php echo '</div>'; echo $after_widget; } function update( $new_instance, $old_instance ) { // Save widget options $instance = array(); $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['categories'] = ( !empty( $new_instance['categories']) ? array_filter(array_map(function($id) { return intval($id); }, (array) $new_instance['categories'])) : ''); $instance['category_d'] = strip_tags( $new_instance['category_d'] ); return $instance; } function form( $instance ) { // Output admin widget options form $title = $instance['title']; // Categories $categories = isset($instance['categories']) ? $instance['categories'] : array(); $category_d = $instance['category_d']; ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> <!-- Category Start --> <!--1. Checkbox Category --> <p>Multi Checkbox Categories</p> <ul style="overflow:hidden"> <?php foreach (get_categories() as $category): ?> <li style="float:left; padding-right:20px;"> <label> <input type="checkbox" class="checkbox" name="<?php echo $this->get_field_name('categories') ?>[]" value="<?php echo $category->cat_ID ?>" <?php checked(in_array($category->cat_ID, $categories)) ?> /> <?php echo $category->name ?> </label> </li> <?php endforeach ?> </ul> <!-- Select Category --> <!-- 2.Category Select Menu --> <p>Select Categories<br/> <select id="<?php echo $this->get_field_id('category_d'); ?>" name="<?php echo $this->get_field_name('category_d'); ?>" class="widefat" style="width:100%;"> <?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?> <?php $option = '<option '. ($term->term_id == $instance['category_d'] ? "selected='selected'":"").'value="' . $term->term_id . '">'; $option .= $term->name; $option .= '</option>'; echo $option; ?> <?php } ?> </select> </p> <!--/ end category --> <?php } } function category_field_register_widgets() { register_widget( 'Category_Fields_Widget' ); } add_action( 'widgets_init', 'category_field_register_widgets' ); ?>
Theme Folder -> function.php
Add this line:
require get_template_directory() . '/inc/category_widget.php'; // inc folder name depend on your theme.