title: Creating a custom categories widget
body: <p>The code below is an attempt to take the WP_Widget_Categories class and use it as the basis for a custom categories widget based on the default categories widget.</p>
  
  <p>I'm getting no output however and the widget is not showing up in the "Available Widgets" listing. What am I doing wrong?</p>
  
  <pre><code>&lt;?php
  /*
  Plugin Name: My Categories Widget
  Version: 1.0
  */
  
  
  class MY_Widget_Categories extends WP_Widget {
  
      function MY_Widget_Categories() {
          $widget_ops = array( 'classname' =&gt; 'widget_categories', 'description' =&gt; __( "A list or dropdown of categories" ) );
          $this-&gt;WP_Widget('categories', __('Categories'), $widget_ops);
      }
  
      function widget( $args, $instance ) {
          extract( $args );
  
          $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title']);
          $c = $instance['count'] ? '1' : '0';
          $h = $instance['hierarchical'] ? '1' : '0';
          $d = $instance['dropdown'] ? '1' : '0';
  
          echo $before_widget;
          if ( $title )
              echo $before_title . $title . $after_title;
  
          $cat_args = array('orderby' =&gt; 'name', 'show_count' =&gt; $c, 'hierarchical' =&gt; $h);
  
          if ( $d ) {
              $cat_args['show_option_none'] = __('Select Category');
              wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
  ?&gt;
  
  &lt;script type='text/javascript'&gt;
  /* &lt;![CDATA[ */
      var dropdown = document.getElementById("cat");
      function onCatChange() {
          if ( dropdown.options[dropdown.selectedIndex].value &gt; 0 ) {
              location.href = "&lt;?php echo get_option('home'); ?&gt;/?cat="+dropdown.options[dropdown.selectedIndex].value;
          }
      }
      dropdown.onchange = onCatChange;
  /* ]]&gt; */
  &lt;/script&gt;
  
  &lt;?php
          } else {
  ?&gt;
          &lt;ul&gt;
  &lt;?php
          $cat_args['title_li'] = '';
          wp_list_categories(apply_filters('widget_categories_args', $cat_args));
  ?&gt;
          &lt;/ul&gt;
  &lt;?php
          }
  
          echo $after_widget;
      }
  
      function update( $new_instance, $old_instance ) {
          $instance = $old_instance;
          $instance['title'] = strip_tags($new_instance['title']);
          $instance['count'] = $new_instance['count'] ? 1 : 0;
          $instance['hierarchical'] = $new_instance['hierarchical'] ? 1 : 0;
          $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
  
          return $instance;
      }
  
      function form( $instance ) {
          //Defaults
          $instance = wp_parse_args( (array) $instance, array( 'title' =&gt; '') );
          $title = esc_attr( $instance['title'] );
          $count = isset($instance['count']) ? (bool) $instance['count'] :false;
          $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
          $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
  ?&gt;
          &lt;p&gt;&lt;label for="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;"&gt;&lt;?php _e( 'Title:' ); ?&gt;&lt;/label&gt;
          &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('title'); ?&gt;" type="text" value="&lt;?php echo $title; ?&gt;" /&gt;&lt;/p&gt;
  
          &lt;p&gt;&lt;input type="checkbox" class="checkbox" id="&lt;?php echo $this-&gt;get_field_id('dropdown'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('dropdown'); ?&gt;"&lt;?php checked( $dropdown ); ?&gt; /&gt;
          &lt;label for="&lt;?php echo $this-&gt;get_field_id('dropdown'); ?&gt;"&gt;&lt;?php _e( 'Show as dropdown' ); ?&gt;&lt;/label&gt;&lt;br /&gt;
  
          &lt;input type="checkbox" class="checkbox" id="&lt;?php echo $this-&gt;get_field_id('count'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('count'); ?&gt;"&lt;?php checked( $count ); ?&gt; /&gt;
          &lt;label for="&lt;?php echo $this-&gt;get_field_id('count'); ?&gt;"&gt;&lt;?php _e( 'Show post counts' ); ?&gt;&lt;/label&gt;&lt;br /&gt;
  
          &lt;input type="checkbox" class="checkbox" id="&lt;?php echo $this-&gt;get_field_id('hierarchical'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('hierarchical'); ?&gt;"&lt;?php checked( $hierarchical ); ?&gt; /&gt;
          &lt;label for="&lt;?php echo $this-&gt;get_field_id('hierarchical'); ?&gt;"&gt;&lt;?php _e( 'Show hierarchy' ); ?&gt;&lt;/label&gt;&lt;/p&gt;
  &lt;?php
      }
  
  }
  
  function my_categories_init()
  {
    register_sidebar_widget(__('My Categories Widget'), 'MY_Widget_Categories');
  }
  
  add_action("plugins_loaded", "my_categories_init");
  ?&gt;
  </code></pre>