Guest User

Untitled

a guest
Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. class Spr_Categories_Widget extends WP_Widget {
  2. public function __construct() {
  3. $widget_options = array(
  4. 'classname' => 'widget_custom_categories_widget',
  5. 'description' => 'This is a Custom Blog Categories Widget',
  6. );
  7. parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
  8. }
  9.  
  10. public function form( $instance ) {
  11. $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
  12. $name = ! empty( $instance['name'] ) ? $instance['name'] : '';
  13. $categories = get_categories( array(
  14. 'orderby' => 'name',
  15. 'parent' => 0
  16. ) );
  17. ?>
  18. <p>
  19. <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
  20. <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 ); ?>" />
  21. </p>
  22. <p>
  23. <label for="Categories">Categories:</label>
  24. </p>
  25. <?php
  26. foreach ( $categories as $category ) {
  27. ?>
  28. <p>
  29. <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 ); ?>" />
  30. </p>
  31. <?php
  32.  
  33. }
  34. ?>
  35.  
  36. <?php
  37.  
  38. }
  39.  
  40. public function update( $new_instance, $old_instance ) {
  41. $instance = $old_instance;
  42. $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
  43. $instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
  44. return $instance;
  45. }
  46.  
  47. public function widget( $args, $instance ) {
  48. $title = apply_filters( 'widget_title', $instance[ 'title' ] );
  49. $category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
  50.  
  51. echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
  52.  
  53. <p><?php echo $category_title ?></p>
  54.  
  55. <?php echo $args['after_widget'];
  56. }
  57.  
  58. }
  59. function spr_custom_categories_widget() {
  60. register_widget( 'Spr_Categories_Widget' );
  61. }
  62. add_action( 'widgets_init', 'spr_custom_categories_widget' );
Add Comment
Please, Sign In to add comment