dongluanguang4774
dongluanguang4774
2015-02-04 14:46

Wordpress自定义小部件不会保存值

已采纳

Hello I am pretty new to Wordpress and I am trying to make a custom widget. I can get the form to display and enter values however whenever I press save, the widget reverts back to it's default values. I have tried adding in the values in accessibility mode also, but to no avail.

Here is a copy of my widget class:

function loadsidewidget()
{
   register_widget('WP_Widget_sidebar_textwidget');
}
add_action('widgets_init', 'loadsidewidget');

class WP_Widget_sidebar_textwidget extends WP_Widget
{

    function __construct() {
        parent::__construct(
                'WP_Widget_sidebar_textwidget',
            __('Sidebar Textwdiget','sidewidget_domain'),
            array( 'description' => __( 'Sample widget' ,'sidewidget_domain'), )
    );
}

public function widget($args, $instance)
{
    extract( $args );
    extract($instance);
    $title = apply_filters('widget_title',$instance['title']);
    $show_info = isset( $instance['textarea'] ) ? $instance['textarea'] :'';
    echo $args['before_widget'];
    if(!empty($title))
    {
        echo $args['before_title'];
        echo '<i class="fa fa-align-left"></i>'.$title;
        echo $args['after_title'];
    }
    echo '<p>'.$instance['textarea'].'</p>';
}

public function update($new_instance, $old_instance)
{
    $instance =$old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['textarea'] = esc_textarea($new_instance['textarea']);
    return $instance;
}

//backend
public function form($instance)
{

    $instance = wp_parse_args(array($intsance),array('title'=>'Title','textarea'=>'default'));
    if(isset($instance['title']))
    {
        $title = strip_tags($instance['title']);
    }
    else
    {
        $title = 'Title';
    }
    $titleId = $this->get_field_id( 'title' );
    $titleName = $this->get_field_name( 'title' );
    $text  = $instance['textarea'];
    $contentId = $this->get_field_id( 'textarea' );
    $contentName = $this->get_field_name( 'textarea' );

    ?>

    <p>
    <label for="<?php echo $titleId; ?>"><?php echo __('Title','sidewidget_domain');?></label>
    <input class="widefat" id="<?php echo $titleId;?>" name="<?php echo $titleName;?>" value="<?php  echo $title;?>"/>
    </p>
    <p>
    <label for="<?php echo $contentId?>"><?php echo __('Content','sidewidget_domain');?></label>
    <textarea class="widefat" id="<?php echo $contentId;?>" name="<?php echo $contentName;?>"><?php echo $text;?></textarea>
    </p>
    <h1><?php echo $instance['textarea'];?></h1>
    <?php 
  } 
}

I have a feeling it's something to do with the base widget Id but I'm not sure.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxiong5972 douxiong5972 6年前

    I figured it out, the problem was this line $instance = wp_parse_args(array($intsance),array('title'=>'Title','textarea'=>'default')); if(isset($instance['title']));, I removed it and it worked.

    点赞 评论 复制链接分享

为你推荐