Php 如何将另一个文本框添加到WP_小部件_文本

Php 如何将另一个文本框添加到WP_小部件_文本,php,wordpress,Php,Wordpress,我不熟悉wordpress和widgets 我正在尝试向默认文本小部件添加另一个字段。你们能帮帮我吗 我正在尝试做的事情的图像: 我能够编辑default-widgets.php并添加另一个textarea,但它不能正常工作。请帮助并指引我正确的方向。 class WP_Widget_Text extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'widg

我不熟悉wordpress和widgets

我正在尝试向默认文本小部件添加另一个字段。你们能帮帮我吗

我正在尝试做的事情的图像:

我能够编辑default-widgets.php并添加另一个
textarea
,但它不能正常工作。请帮助并指引我正确的方向。

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
    }
}
class WP\u Widget\u文本扩展WP\u Widget{
函数_u构造(){
$widget_ops=array('classname'=>'widget_text','description'=>'widget_\'('任意文本或HTML');
$control_ops=阵列('宽度'=>400,'高度'=>350);
父项::_构造('text',_uu('text'),$widget_ops,$control_ops);
}
函数小部件($args$instance){
摘录($args);
$title=apply_过滤器('widget_title',空($instance['title'])?“”:$instance['title'],$instance$this->id_base);
$text=apply_过滤器('widget_text',空($instance['text'])?“”:$instance['text'],$instance);
$text=apply_过滤器('widget_text2',空($instance['text2'])?“”:$instance['text2'],$instance);
echo$before_小部件;
如果(!empty($title)){echo$before_title.$title.$before_title;}?>

我不确定你是否明白了,但这里有一个解决你问题的方法

首先,您不想直接编辑小部件。如果您更新Wordpress,您的代码将被覆盖。Wordpress具有高度可扩展性,因此您永远不必直接编辑代码

理想情况下,你应该创建一个插件

Wordpress的另一个优点是他们有非常好的文档

但是,为了简单起见,我们将在您使用的主题中的functions.php中执行此操作。您可以在
/wp content/themes/your_theme/functions.php

以下是您问题中代码示例的修改版本:

//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" );' ) );
请注意,您在这里设置了两次
$text=
。因此您基本上重写了text的值,并将其替换为text2。然后将text2保留为空


如果这不是您的问题,那么它可能与将小部件注册为“文本”有关,因为这是由WP\u widget\u text执行的。这不太可能,因为我假设您直接编辑了WP\u widget\u text类。

这属于什么不起作用?您没有显示该小部件内容吗?您可以解释更多吗?
$text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
$text = apply_filters( 'widget_text2', empty( $instance['text2'] ) ? '' : $instance['text2'], $instance );