How to add another text box to WP_Widget_Text

admin

Administrator
Staff member
I am new to wordpress and widgets.

I am trying to add another field to the default text widget. Can you guys help me out.

An image of what i am trying to do:
<a href=" " rel="nofollow"> </a>

<strong>I was able to edit default-widgets.php and add another
Code:
textarea
but it does not work properly. please help and guide me to the right direction.</strong>

Code:
class WP_Widget_Text extends WP_Widget {

    function __construct() {
        $widget_ops = array('classname' =&gt; 'widget_text', 'description' =&gt; __('Arbitrary text or HTML'));
        $control_ops = array('width' =&gt; 400, 'height' =&gt; 350);
        parent::__construct('text', __('Text'), $widget_ops, $control_ops);
    }

    function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this-&gt;id_base );
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        $text = apply_filters( 'widget_text2', empty( $instance['text2'] ) ? '' : $instance['text2'], $instance );
        echo $before_widget;
        if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?&gt;
            &lt;div class="textwidget"&gt;
                &lt;?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?&gt;
            &lt;/div&gt;
            &lt;div class="textwidget2"&gt;
                &lt;?php echo !empty( $instance['filter'] ) ? wpautop( $text2 ) : $text2; ?&gt;
            &lt;/div&gt;
        &lt;?php
        echo $after_widget;
    }

    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'];
            $instance['text2'] =  $new_instance['text2'];
        } else {
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
            $instance['text2'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text2']) ) ); // wp_filter_post_kses() expects slashed
        }
        $instance['filter'] = isset($new_instance['filter']);
        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' =&gt; '', 'text' =&gt; '', 'text2' =&gt; '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
        $text2 = esc_textarea($instance['text2']);
?&gt;
        &lt;p&gt;&lt;label for="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;"&gt;&lt;?php _e('Title:'); ?&gt;&lt;/label&gt;
        &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('title'); ?&gt;" type="text" value="&lt;?php echo esc_attr($title); ?&gt;" /&gt;&lt;/p&gt;

        &lt;textarea class="widefat" rows="16" cols="20" id="&lt;?php echo $this-&gt;get_field_id('text'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('text'); ?&gt;"&gt;&lt;?php echo $text; ?&gt;&lt;/textarea&gt;
        &lt;textarea class="widefat" rows="8" cols="20" id="&lt;?php echo $this-&gt;get_field_id('text2'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('text2'); ?&gt;"&gt;&lt;?php echo $text2; ?&gt;&lt;/textarea&gt;

        &lt;p&gt;&lt;input id="&lt;?php echo $this-&gt;get_field_id('filter'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('filter'); ?&gt;" type="checkbox" &lt;?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?&gt; /&gt;&amp;nbsp;&lt;label for="&lt;?php echo $this-&gt;get_field_id('filter'); ?&gt;"&gt;&lt;?php _e('Automatically add paragraphs'); ?&gt;&lt;/label&gt;&lt;/p&gt;
&lt;?php
    }
}