Title: Creating a new widget
Last modified: August 30, 2016

---

# Creating a new widget

 *  Resolved [Dot22](https://wordpress.org/support/users/dot22/)
 * (@dot22)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/creating-a-new-widget/)
 * Hello everyone! I’m working on a site with WordPress 4.3 and want to make a new
   text widget but I need to include some default text before and after the paragraph.
 * For example, the widget have two fields: Title and Text. If user complete text
   with “Hello world” I need to display “Hey, Hello world! :)” on the front side
   of the site. So, the “Hey,” must appear before and the “! :)” after. I’ve this
   code but don’t know how to do that:
 *     ```
       class eit_wdgt_widget extends WP_Widget {
   
       	function __construct() {
       		parent::__construct(
       			'eit_wdgt_widget', // Base ID
       			__( 'New Widget', 'text_domain' ), // Name
       			array( 'description' => __( 'Add a new TEXT widget.', 'text_domain' ), ) // Args
       		);
       	}
   
       	public function widget( $args, $instance ) {
       	$text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
       	$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
       		echo $args['before_eiwdgt_widget'];
       		if ( ! empty( $instance['title'] ) ) {
       			echo $args['before_eitwdgt_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_eitwdgt_title'];
       		} ?>
       		<?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
       		<?php
       		echo $args['after_eitwdgt_widget'];
       	}
   
       		public function update( $new_instance, $old_instance ) {
       		$instance = $old_instance;
       		$instance['title'] = strip_tags($new_instance['title']);
       		if ( current_user_can('unfiltered_html') )
       			$instance['text'] =  $new_instance['text'];
       		else
       			$instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
       		$instance['filter'] = ! empty( $new_instance['filter'] );
       		return $instance;
       	}
   
       	public function form( $instance ) {
       		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
       		$title = strip_tags($instance['title']);
       		$text = esc_textarea($instance['text']);
       ?>
       		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Titulo del video:'); ?></label>
       		<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>
   
       		<textarea class="widefat" rows="2" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
   
       <?php
       	}
       } // class eit_wdgt_widget
       // register eit_wdgt_widget
       function register_eit_wdgt_widget() {
           register_widget( 'eit_wdgt_widget' );
       }
       add_action( 'widgets_init', 'register_eit_wdgt_widget' );
       ```
   
 * Thanks in advance! =)

Viewing 1 replies (of 1 total)

 *  Thread Starter [Dot22](https://wordpress.org/support/users/dot22/)
 * (@dot22)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/creating-a-new-widget/#post-6797610)
 * I found the solution. The content must be added before and after this line:
 * `<?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>`

Viewing 1 replies (of 1 total)

The topic ‘Creating a new widget’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Dot22](https://wordpress.org/support/users/dot22/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/creating-a-new-widget/#post-6797610)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
