WP_Widget_Text

Core class used to implement a Text widget.

Defined (1)

The class is defined in the following location(s).

/wp-includes/widgets/class-wp-widget-text.php  
  1. class WP_Widget_Text extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up a new Text widget instance. 
  5. * @since 2.8.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $widget_ops = array( 
  10. 'classname' => 'widget_text',  
  11. 'description' => __( 'Arbitrary text or HTML.' ),  
  12. 'customize_selective_refresh' => true,  
  13. ); 
  14. $control_ops = array( 'width' => 400, 'height' => 350 ); 
  15. parent::__construct( 'text', __( 'Text' ), $widget_ops, $control_ops ); 
  16.  
  17. /** 
  18. * Outputs the content for the current Text widget instance. 
  19. * @since 2.8.0 
  20. * @access public 
  21. * @param array $args Display arguments including 'before_title', 'after_title',  
  22. * 'before_widget', and 'after_widget'. 
  23. * @param array $instance Settings for the current Text widget instance. 
  24. */ 
  25. public function widget( $args, $instance ) { 
  26.  
  27. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  28. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); 
  29.  
  30. $widget_text = ! empty( $instance['text'] ) ? $instance['text'] : ''; 
  31.  
  32. /** 
  33. * Filters the content of the Text widget. 
  34. * @since 2.3.0 
  35. * @since 4.4.0 Added the `$this` parameter. 
  36. * @param string $widget_text The widget content. 
  37. * @param array $instance Array of settings for the current widget. 
  38. * @param WP_Widget_Text $this Current Text widget instance. 
  39. */ 
  40. $text = apply_filters( 'widget_text', $widget_text, $instance, $this ); 
  41.  
  42. echo $args['before_widget']; 
  43. if ( ! empty( $title ) ) { 
  44. echo $args['before_title'] . $title . $args['after_title']; 
  45. } ?> 
  46. <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div> 
  47. <?php 
  48. echo $args['after_widget']; 
  49.  
  50. /** 
  51. * Handles updating settings for the current Text widget instance. 
  52. * @since 2.8.0 
  53. * @access public 
  54. * @param array $new_instance New settings for this instance as input by the user via 
  55. * WP_Widget::form(). 
  56. * @param array $old_instance Old settings for this instance. 
  57. * @return array Settings to save or bool false to cancel saving. 
  58. */ 
  59. public function update( $new_instance, $old_instance ) { 
  60. $instance = $old_instance; 
  61. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  62. if ( current_user_can( 'unfiltered_html' ) ) { 
  63. $instance['text'] = $new_instance['text']; 
  64. } else { 
  65. $instance['text'] = wp_kses_post( $new_instance['text'] ); 
  66. $instance['filter'] = ! empty( $new_instance['filter'] ); 
  67. return $instance; 
  68.  
  69. /** 
  70. * Outputs the Text widget settings form. 
  71. * @since 2.8.0 
  72. * @access public 
  73. * @param array $instance Current settings. 
  74. */ 
  75. public function form( $instance ) { 
  76. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) ); 
  77. $filter = isset( $instance['filter'] ) ? $instance['filter'] : 0; 
  78. $title = sanitize_text_field( $instance['title'] ); 
  79. ?> 
  80. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
  81. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> 
  82.  
  83. <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label> 
  84. <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo esc_textarea( $instance['text'] ); ?></textarea></p> 
  85.  
  86. <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p> 
  87. <?php