Creating a simple widget
-
I followed the new wordpress widget API and did this simple widget:
class CategoryWidget extends WP_Widget{ function CategoryWidget(){ $widget_ops = array('classname' => 'categories', 'description' => __( "Theme-default category block") ); $this->WP_Widget('categories', __('My Categories',), $widget_ops); } function widget($args, $instance){ extract($args); $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']); $orderby = empty( $instance['orderby'] ) ? 'name' : $instance['orderby']; $showcount = isset($instance['showcount']) ? $instance['showcount'] : false; echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; // content...... echo $after_widget; } function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = strip_tags(stripslashes($new_instance['title'])); if ( in_array( $new_instance['orderby'], array( 'name', 'ID', 'slug','count' ) ) ) { $instance['orderby'] = $new_instance['orderby']; } else { $instance['orderby'] = 'name'; } if ( isset($new_instance['showcount']) ) $instance['showcount'] = 1; return $instance; } function form($instance){ // defaults $instance = wp_parse_args( (array) $instance, array('orderby' => 'name', 'title'=>'Categories', 'showcount' => false) ); $title = htmlspecialchars($instance['title']); echo '<p><label for="' . $this->get_field_name('title') . '">' . __('Title:',) . ' <br /><input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></label></p>'; ?> <p> <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort categories by:', ); ?></label> <select name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>" class="widefat"> <option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e('By name (alphabetically)',); ?></option> <option value="ID"<?php selected( $instance['orderby'], 'ID' ); ?>><?php _e('By category ID',); ?></option> <option value="slug"<?php selected( $instance['orderby'], 'slug' ); ?>><?php _e('By category slug',); ?></option> <option value="count"<?php selected( $instance['orderby'], 'count' ); ?>><?php _e( 'By post count', ); ?></option> </select> </p> <label for="<?php echo $this->get_field_id('showcount'); ?>"><?php _e('Show Link showcount',); ?></label> <input class="checkbox" type="checkbox" <?php checked($instance['showcount'], true) ?> id="<?php echo $this->get_field_id('showcount'); ?>" name="<?php echo $this->get_field_name('showcount'); ?>" /> <?php } } function MyWidgetsInit(){ register_widget('CategoryWidget'); } add_action('widgets_init', 'MyWidgetsInit');
the problem is that I can’t update some of the widget options from the dashboard. I can only set the title. “sortby” and “showcount” fields don’t work, they just revert to the default value after I press save…
I checked the code over and over again, and see no reason why it shouldn’t work…
did I miss something?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Creating a simple widget’ is closed to new replies.