小部件输出值,但不保存文本区域内的任何内容

时间:2017-04-11 作者:Ashiquzzaman Kiron

就我个人而言,我不明白为什么textarea没有保存textarea内部的值,而是在前端显示值。我也检查了这个问题-question 但我似乎找不到我的小部件有什么问题。任何帮助都将不胜感激

//register widget backend
    class resumee_experience1_widget extends WP_Widget{


  public function __construct(){
    parent::__construct(\'experience_one_widget\', __(\'Resume Experience Widget\', \'resumee\'), array(
        \'description\' => __(\'Resume Experience Widget\', \'resumee\'),
        \'customize_selective_refresh\' => true
      ));
  }


  /**
   * Front-end display of widget.
   *
   * @see WP_Widget::widget()
   *
   * @param array $args     Widget arguments.
   * @param array $instance Saved values from database.
   */
  public function widget($args, $instance){

        $title = apply_filters(\'widget_title\', $instance[\'title\'] );
        $exp1 = $instance[\'exp1\'];
        $jd1 = $instance[\'jd1\'];
        $exp2 = $instance[\'exp2\'];
        $jd2 = $instance[\'jd2\'];
        $exp3 = $instance[\'exp3\'];
        $jd3 = $instance[\'jd3\'];



       /* Before widget (defined by themes). */
      echo $args[\'before_widget\'] ;

        echo \'<div class="container experience">
                 <div class="col-md-12">\';

        if(isset($title) && !empty($title)){

          echo \'<h3 class="timeline">\'. esc_html__($title) .\'</h3>\';

        }

        echo \'<ul class="timeline"> 
                  <li class="timeline-inverted">
                   <div class="timeline-badge warning">●</div>
                    <div class="timeline-panel">
                     <div class="timeline-heading">\';

        if(isset($exp1) && !empty($exp1)){

           echo \'<h4 class="timeline-title">\'. esc_html__($exp1) .\'</h4>\';
        }

        echo \'</div><div class="timeline-body">\';


        if(isset($jd1) && !empty($jd1)){

           echo \'<p>\'. esc_html__($jd1) .\'</p>\';
        }

        echo \'</div></div></li>
               <li class="timeline-inverted">
                 <div class="timeline-badge warning">●</div>
                  <div class="timeline-panel">
                   <div class="timeline-heading">\';

         if(isset($exp2) && !empty($exp2)){

           echo \'<h4 class="timeline-title">\'. esc_html__($exp2) .\'</h4>\';
        }

        echo \'</div><div class="timeline-body">\';


        if(isset($jd2) && !empty($jd2)){

           echo \'<p>\'. esc_html__($jd2) .\'</p>\';
        }

        echo \'</div></div></li>
              <li class="timeline-inverted">
                  <div class="timeline-badge warning">●</div>
                   <div class="timeline-panel">
                    <div class="timeline-heading">\';

        if(isset($exp3) && !empty($exp3)){

           echo \'<h4 class="timeline-title">\'. esc_html__($exp3) .\'</h4>\';
        }

        echo \'</div><div class="timeline-body">\';


        if(isset($jd3) && !empty($jd3)){

           echo \'<p>\'. esc_html__($jd3) .\'</p>\';
        }

        echo \'</div></div></li></ul></div></div>\';

      /* After widget (defined by themes). */
       echo $args[\'after_widget\'] ;
  }

/**
 * Back-end widget form.
 *
 * @see WP_Widget::form()
 *
 * @param array $instance Previously saved values from database.
 */
  public function form($instance){

         /* Set up some default widget settings. */
          $defaults = array( 
             \'title\' =>  __(\'Work Experience\', \'resumee\'), 
             \'exp1\' => __(\'Company Name, Jan, 2015 - present\', \'resumee\'), 
             \'jd1\' => __(\'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam porttitor dapibus ipsum ut efficitur. Aliquam feugiat nec sem dapibus blandit. Nam non faucibus urna, at pulvinar nisl. Aliquam erat volutpat. Ut eget aliquet diam. In et Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam porttitor dapibus ipsum ut efficitur.\', \'resumee\'), 
             \'exp2\' => __(\'Company Name, Feb, 2013 - 2015\', \'resumee\'), 
             \'jd2\' => __(\'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam porttitor dapibus ipsum ut efficitur. Aliquam feugiat nec sem dapibus blandit. Nam non faucibus urna, at pulvinar nisl. Aliquam erat volutpat. Ut eget aliquet diam. In et Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam porttitor dapibus ipsum ut efficitur.\', \'resumee\'), 
             \'exp3\' => __(\'Company Name, March, 2011 - 2013\', \'resumee\'), 
             \'jd3\' => __(\'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam porttitor dapibus ipsum ut efficitur. Aliquam feugiat nec sem dapibus blandit. Nam non faucibus urna, at pulvinar nisl. Aliquam erat volutpat. Ut eget aliquet diam. In et Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam porttitor dapibus ipsum ut efficitur.\', \'resumee\')
             );
          $instance = wp_parse_args( (array) $instance, $defaults ); 

    ?>

  <!-- title field -->
    <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\' , \'resumee\'); ?></label>
    <br>
      <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" value="<?php echo esc_attr( $instance[\'title\'] ); ?>" type="text">
    <br>
    <br>


  <!-- exp1 field -->
    <label for="<?php echo $this->get_field_id(\'exp1\'); ?>"><?php _e(\'Company:\' , \'resumee\'); ?></label>
    <br>
      <input class="widefat" id="<?php echo $this->get_field_id(\'exp1\'); ?>" name="<?php echo $this->get_field_name(\'exp1\'); ?>" value="<?php echo esc_attr( $instance[\'exp1\'] ); ?>" type="text">

    <br>
    <br>


  <!-- jd1 field -->
    <label for="<?php echo $this->get_field_id(\'jd1\'); ?>"><?php _e(\'Job Description:\' , \'resumee\'); ?></label>
    <br>
      <textarea class="widefat" rows="10" cols="20" id="<?php echo $this->get_field_id(\'jd1\'); ?>" name="<?php echo $this->get_field_name(\'jd1\'); ?>" ><?php echo ($instance[\'$jd1\']); ?></textarea>

    <br>
    <br>



  <!-- exp2 field -->
    <label for="<?php echo $this->get_field_id(\'exp2\'); ?>"><?php _e(\'Company:\' , \'resumee\'); ?></label>
    <br>
      <input class="widefat" id="<?php echo $this->get_field_id(\'exp2\'); ?>" name="<?php echo $this->get_field_name(\'exp2\'); ?>" value="<?php echo esc_attr( $instance[\'exp2\'] ); ?>" type="text">
    <br>
    <br>


  <!-- jd2 field -->
    <label for="<?php echo $this->get_field_id(\'jd2\'); ?>"><?php _e(\'Job Description:\' , \'resumee\'); ?></label>
    <br>
      <textarea class="widefat" rows="10" cols="20" id="<?php echo $this->get_field_id(\'jd2\'); ?>" name="<?php echo $this->get_field_name(\'jd2\'); ?>" ><?php echo ($instance[\'$jd2\']); ?></textarea>
    <br>
    <br>



  <!-- exp3 field -->
    <label for="<?php echo $this->get_field_id(\'exp3\'); ?>"><?php _e(\'Company:\' , \'resumee\'); ?></label>
    <br>
      <input class="widefat" id="<?php echo $this->get_field_id(\'exp3\'); ?>" name="<?php echo $this->get_field_name(\'exp3\'); ?>" value="<?php echo esc_attr( $instance[\'exp3\'] ); ?>" type="text">
    <br>
    <br>


   <!-- jd3 field -->
    <label for="<?php echo $this->get_field_id(\'jd3\'); ?>"><?php _e(\'Job Description:\' , \'resumee\'); ?></label>
    <br>
      <textarea class="widefat" rows="10" cols="20" id="<?php echo $this->get_field_id(\'jd3\'); ?>" name="<?php echo $this->get_field_name(\'jd3\'); ?>" ><?php echo ($instance[\'$jd3\']); ?></textarea>
    <br>
    <br>



    <?php 
  }


    /**
       * Sanitize widget form values as they are saved.
       *
       * @see WP_Widget::update()
       *
       * @param array $new_instance Values just sent to be saved.
       * @param array $old_instance Previously saved values from database.
       *
       * @return array Updated safe values to be saved.
    */
     public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance[ \'title\' ] = strip_tags( $new_instance[ \'title\' ] );
        $instance[ \'exp1\' ] = strip_tags( $new_instance[ \'exp1\' ] );
        $instance[ \'jd1\' ] = strip_tags( $new_instance[ \'jd1\' ] );
        $instance[ \'exp2\' ] = strip_tags( $new_instance[ \'exp2\' ] );
        $instance[ \'jd2\' ] = strip_tags( $new_instance[ \'jd2\' ] );
        $instance[ \'exp3\' ] = strip_tags( $new_instance[ \'exp3\' ] );
        $instance[ \'jd3\' ] = strip_tags( $new_instance[ \'jd3\' ] );

        return $instance;
     }

}


//regoster widget with hook
function register_experience_one_widget(){
  register_widget( \'resumee_experience1_widget\' );
}
add_action( \'widgets_init\', \'register_experience_one_widget\');

1 个回复
最合适的回答,由SO网友:frenchy black 整理而成

改变


echo ($instance[\'$jd1\']); 
echo ($instance[\'$jd2\']); 
echo ($instance[\'$jd3\']); 


echo ($instance[\'jd1\']); 
echo ($instance[\'jd2\']); 
echo ($instance[\'jd3\']); 
所以基本上移除$

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。