Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Spr_Categories_Widget extends WP_Widget {
- public function __construct() {
- $widget_options = array(
- 'classname' => 'widget_custom_categories_widget',
- 'description' => 'This is a Custom Blog Categories Widget',
- );
- parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
- }
- public function form( $instance ) {
- $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
- $name = ! empty( $instance['name'] ) ? $instance['name'] : '';
- $categories = get_categories( array(
- 'orderby' => 'name',
- 'parent' => 0
- ) );
- ?>
- <p>
- <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
- <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
- </p>
- <p>
- <label for="Categories">Categories:</label>
- </p>
- <?php
- foreach ( $categories as $category ) {
- ?>
- <p>
- <input type="text" id="<?php echo $this->get_field_id($category->slug); ?>" class="widefat" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo esc_attr( $name ); ?>" />
- </p>
- <?php
- }
- ?>
- <?php
- }
- public function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
- $instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
- return $instance;
- }
- public function widget( $args, $instance ) {
- $title = apply_filters( 'widget_title', $instance[ 'title' ] );
- $category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
- echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
- <p><?php echo $category_title ?></p>
- <?php echo $args['after_widget'];
- }
- }
- function spr_custom_categories_widget() {
- register_widget( 'Spr_Categories_Widget' );
- }
- add_action( 'widgets_init', 'spr_custom_categories_widget' );
Add Comment
Please, Sign In to add comment