3 * Widget API: WP_Widget_Text class
11 * Core class used to implement a Text widget.
17 class WP_Widget_Text extends WP_Widget {
20 * Sets up a new Text widget instance.
25 public function __construct() {
27 'classname' => 'widget_text',
28 'description' => __( 'Arbitrary text or HTML.' ),
29 'customize_selective_refresh' => true,
31 $control_ops = array( 'width' => 400, 'height' => 350 );
32 parent::__construct( 'text', __( 'Text' ), $widget_ops, $control_ops );
36 * Outputs the content for the current Text widget instance.
41 * @param array $args Display arguments including 'before_title', 'after_title',
42 * 'before_widget', and 'after_widget'.
43 * @param array $instance Settings for the current Text widget instance.
45 public function widget( $args, $instance ) {
47 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
48 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
50 $widget_text = ! empty( $instance['text'] ) ? $instance['text'] : '';
53 * Filters the content of the Text widget.
56 * @since 4.4.0 Added the `$this` parameter.
58 * @param string $widget_text The widget content.
59 * @param array $instance Array of settings for the current widget.
60 * @param WP_Widget_Text $this Current Text widget instance.
62 $text = apply_filters( 'widget_text', $widget_text, $instance, $this );
64 echo $args['before_widget'];
65 if ( ! empty( $title ) ) {
66 echo $args['before_title'] . $title . $args['after_title'];
68 <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
70 echo $args['after_widget'];
74 * Handles updating settings for the current Text widget instance.
79 * @param array $new_instance New settings for this instance as input by the user via
81 * @param array $old_instance Old settings for this instance.
82 * @return array Settings to save or bool false to cancel saving.
84 public function update( $new_instance, $old_instance ) {
85 $instance = $old_instance;
86 $instance['title'] = sanitize_text_field( $new_instance['title'] );
87 if ( current_user_can( 'unfiltered_html' ) ) {
88 $instance['text'] = $new_instance['text'];
90 $instance['text'] = wp_kses_post( $new_instance['text'] );
92 $instance['filter'] = ! empty( $new_instance['filter'] );
97 * Outputs the Text widget settings form.
102 * @param array $instance Current settings.
104 public function form( $instance ) {
105 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
106 $filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
107 $title = sanitize_text_field( $instance['title'] );
109 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
110 <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>
112 <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
113 <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>
115 <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>