• Resolved Dot22

    (@dot22)


    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

    (@dot22)

    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.