Skip to content

Instantly share code, notes, and snippets.

@ruturajpatki
Last active June 28, 2019 23:15
Show Gist options
  • Save ruturajpatki/a3974e509b310f346cac2bfc9f60f2bd to your computer and use it in GitHub Desktop.
Save ruturajpatki/a3974e509b310f346cac2bfc9f60f2bd to your computer and use it in GitHub Desktop.
Custom Widget to list WordPress categories along with the Post count. Requires styling with CSS to blend it well with theme.
<?php
// Adds widget: Categories
class RutuCategories_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'rutu_categories_widget',
esc_html__( 'Rutu Categories', 'mfl' ),
array( 'description' => esc_html__( 'Custom Categories widget.', 'mfl' ), ) // Args
);
}
private $widget_fields = array(
array(
'label' => 'IDs of Categories to exclude',
'id' => 'cw_exclude',
'default' => '1',
'type' => 'text',
),
);
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'childless' => true,
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => false,
'exclude' => $instance['cw_exclude'],
) );
echo '<ul class="categories">';
foreach($terms as $category) {
echo '<li><a href="'. get_term_link($category->term_id) . '">'. $category->name . ' <span>('. $category->count . ')</span></a></li>';
}
echo '</ul>';
echo $args['after_widget'];
}
public function field_generator( $instance ) {
$output = '';
foreach ( $this->widget_fields as $widget_field ) {
$default = '';
if ( isset($widget_field['default']) ) {
$default = $widget_field['default'];
}
$widget_value = ! empty( $instance[$widget_field['id']] ) ? $instance[$widget_field['id']] : esc_html__( $default, 'mfl' );
switch ( $widget_field['type'] ) {
default:
$output .= '<p>';
$output .= '<label for="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'">'.esc_attr( $widget_field['label'], 'mfl' ).':</label> ';
$output .= '<input class="widefat" id="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'" name="'.esc_attr( $this->get_field_name( $widget_field['id'] ) ).'" type="'.$widget_field['type'].'" value="'.esc_attr( $widget_value ).'">';
$output .= '</p>';
}
}
echo $output;
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'mfl' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'mfl' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
$this->field_generator( $instance );
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
foreach ( $this->widget_fields as $widget_field ) {
switch ( $widget_field['type'] ) {
default:
$instance[$widget_field['id']] = ( ! empty( $new_instance[$widget_field['id']] ) ) ? strip_tags( $new_instance[$widget_field['id']] ) : '';
}
}
return $instance;
}
}
function register_rutu_categories_widget() {
register_widget( 'RutuCategories_Widget' );
}
add_action( 'widgets_init', 'register_rutu_categories_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment