Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:00:43+00:00 2026-06-01T08:00:43+00:00

I am new to wordpress and widgets. I am trying to add another field

  • 0

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:
https://i.stack.imgur.com/fkKgc.jpg

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

class WP_Widget_Text extends WP_Widget {

    function __construct() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
        $control_ops = array('width' => 400, 'height' => 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->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; } ?>
            <div class="textwidget">
                <?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
            </div>
            <div class="textwidget2">
                <?php echo !empty( $instance['filter'] ) ? wpautop( $text2 ) : $text2; ?>
            </div>
        <?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' => '', 'text' => '', 'text2' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
        $text2 = esc_textarea($instance['text2']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></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="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
        <textarea class="widefat" rows="8" cols="20" id="<?php echo $this->get_field_id('text2'); ?>" name="<?php echo $this->get_field_name('text2'); ?>"><?php echo $text2; ?></textarea>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:00:44+00:00Added an answer on June 1, 2026 at 8:00 am

    I’m not sure if you figured out yet, but here is a solution to your problem.

    The first thing is you don’t want to edit the widget directly. If you update WordPress, your code will be overwritten. WordPress is highly extensible, so you never have to edit the code directly.

    Ideally, you should create a plugin. If you want to learn how, read this http://codex.wordpress.org/Writing_a_Plugin

    Another great thing about WordPress is that they have very good documentation.

    However, for simplicity we will just do this in functions.php in the theme you are using. You can find it in /wp-content/themes/your_theme/functions.php

    Here is the modified version of the code sample in your question:

    //First we need to change the name of the Widget. I just named it RT_Widget_Text, so just be aware that you can change it to anything you want and to replace all instances of that text. Just make sure to not name it WP_Widget_Text
    class RT_Widget_Text extends WP_Widget {
    
        function __construct() {
            //I made a change here. I changed the class name to widget_double_text. This probably has no effect, but I just changed it for good measure.
            $widget_ops = array('classname' => 'widget_double_text', 'description' => __('Arbitrary text or HTML'));
            $control_ops = array('width' => 400, 'height' => 350);
    
            //Here is the important part. Change the "text" to "double_text" and "Text" to "Text 2" or to some other text that will identify the widget.
            parent::__construct('double_text', __('Text 2'), $widget_ops, $control_ops);
        }
    
        function widget( $args, $instance ) {
            extract($args);
            $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
            $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
            //I changed this to $text2 = instead of $text =
            //This would have caused the first text section to display you text2 and your text2 to display nothing.
            //I wonder if this was your issue?
            $text2 = apply_filters( 'widget_text', empty( $instance['text2'] ) ? '' : $instance['text2'], $instance );
            echo $before_widget;
            if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
                <div class="textwidget">
                    <?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
                </div>
                <div class="textwidget2">
                    <?php echo !empty( $instance['filter'] ) ? wpautop( $text2 ) : $text2; ?>
                </div>
            <?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' => '', 'text' => '', 'text2' => '' ) );
            $title = strip_tags($instance['title']);
            $text = esc_textarea($instance['text']);
            $text2 = esc_textarea($instance['text2']);
    ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></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="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
            <textarea class="widefat" rows="8" cols="20" id="<?php echo $this->get_field_id('text2'); ?>" name="<?php echo $this->get_field_name('text2'); ?>"><?php echo $text2; ?></textarea>
    
            <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
    <?php
        }
    }
    //Now you need to register your widget. Remember to change it to whatever you named your widget
    add_action( 'widgets_init', create_function( '', 'register_widget( "RT_Widget_Text" );' ) );
    

    With all that said, I think your bug may have been a simple typo in your widget( $args, instance) method here:

    $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
    $text = apply_filters( 'widget_text2', empty( $instance['text2'] ) ? '' : $instance['text2'], $instance );
    

    Notice that you are setting $text = twice here. So you basically overwrote the value of text and replaced it with text2. And then that left text2 null.

    If that was not your problem, then it may have to do with registering the widget as “text” since that is taken by WP_Widget_Text. This is unlikely since I’m assuming you edited the WP_Widget_Text class directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to WordPress, but need to use it so I can add
I'm developing a new Wordpress widget. Its configuration form in the widgets admin panel
I'm a little new to Wordpress, and I'm using one of the default theme.
I'm having some problems trying to format an If/Then condition in a Wordpress widget.
I'm new to the Wordpress plugin and widget APIs, but I'm sure this is
I'm pretty new to wordpress, html, css, and javascript and would love your help
I'm trying to find the proper way to add a custom widget to the
I'm new to wordpress development and created a widget that works fine, however I'm
I'm trying to work out how to add a first and last class on
I'm trying to create a Widget in Wordpress, and I'm running into an issue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.