1

我按照指南创建了一个 WordPress 插件,该插件会在您的页面上显示一个 youtube 子按钮。

在指南中它完美地工作,但由于某种原因它对我不起作用。

这是我的问题:当我在 widget() 中使用 var_dump($instance) 时,它会打印出一个空数组,但是当我尝试在我的 form() 中打印_r($instance) 时,它会打印出正确的信息

这是小部件文件:

<?php
/**
 * Adds Youtube_Subs widget.
 */
class Youtube_Subs_Widget extends WP_Widget {

/**
 * Register widget with WordPress.
 */
function __construct() {
  parent::__construct(
    'youtubesubs_widget', // Base ID
    esc_html__( 'YouTube Subs', 'mp_domain' ), // Name
    array( 'description' => esc_html__( 'Widget to display YouTube subs', 'mp_domain' ), ) // Args
  );
}

/**
 * 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 ) {
  echo $args['before_widget']; // Whatever you want to display before widget (<div>, etc)
  if ( ! empty( $instance['title'] ) ) {
    echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
  }

  // Widget Content Output
  var_dump($instance);
  // echo '<div class="g-ytsubscribe" data-channel="'.$instance['channel'].'" data-layout="'.$instance['layout'].'" data-count="'.$instance['count'].'"></div>';

  echo $args['after_widget']; // Whatever you want to display after widget (</div>, etc)
}

/**
 * Back-end widget form.
 *
 * @see WP_Widget::form()
 *
 * @param array $instance Previously saved values from database.
 */
public function form( $instance ) {
  $title = (! empty( $instance['title'] ) ? $instance['title'] : esc_attr( 'YouTube Subs', 'mp_domain' )); 
  
  $channel = (! empty( $instance['channel'] ) ? $instance['channel'] : esc_attr( 'techguyweb', 'mp_domain' )); 

  $layout = (! empty( $instance['layout'] ) ? $instance['layout'] : esc_attr( 'default', 'mp_domain' )); 

  $count = (! empty( $instance['count'] ) ? $instance['count'] : esc_attr( 'default', 'mp_domain' )); 

  ?>
  
  <!-- TITLE -->
  <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
      <?php esc_attr_e( 'Title:', 'mp_domain' ); ?>
    </label> 

    <input 
      class="widefat" 
      id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" 
      name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" 
      type="text" 
      value="<?php echo esc_attr( $title ); ?>">
  </p>

  <!-- CHANNEL -->
  <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'channel' ) ); ?>">
      <?php esc_attr_e( 'Channel:', 'mp_domain' ); ?>
    </label> 

    <input 
      class="widefat" 
      id="<?php echo esc_attr( $this->get_field_id( 'channel' ) ); ?>" 
      name="<?php echo esc_attr( $this->get_field_name( 'channel' ) ); ?>" 
      type="text" 
      value="<?php echo esc_attr( $channel ); ?>">
  </p>

  <!-- LAYOUT -->
  <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'layout' ) ); ?>">
      <?php esc_attr_e( 'Layout:', 'mp_domain' ); ?>
    </label> 

    <select 
      class="widefat" 
      id="<?php echo esc_attr( $this->get_field_id( 'layout' ) ); ?>" 
      name="<?php echo esc_attr( $this->get_field_name( 'layout' ) ); ?>">
      <option value="default" <?php echo ($layout == 'default') ? 'selected' : ''; ?>>
        Default
      </option>
      <option value="full" <?php echo ($layout == 'full') ? 'selected' : ''; ?>>
        Full
      </option>
    </select>
  </p>

  <!-- COUNT -->
  <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>">
      <?php esc_attr_e( 'Count:', 'mp_domain' ); ?>
    </label> 

    <select 
      class="widefat" 
      id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" 
      name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>">
      <option value="default" <?php echo ($count == 'default') ? 'selected' : ''; ?>>
        Default
      </option>
      <option value="hidden" <?php echo ($count == 'hidden') ? 'selected' : ''; ?>>
        Hidden
      </option>
    </select>
  </p>
  <?php 

  print_r($instance);
}

/**
 * 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'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

  $instance['channel'] = ( ! empty( $new_instance['channel'] ) ) ? strip_tags( $new_instance['channel'] ) : '';

  $instance['layout'] = ( ! empty( $new_instance['layout'] ) ) ? strip_tags( $new_instance['layout'] ) : '';

  $instance['count'] = ( ! empty( $new_instance['count'] ) ) ? strip_tags( $new_instance['count'] ) :      '';
   
       return $instance;
 }
}

我在我注册小部件的地方连接了我的插件文件:

// Register Widget
 function register_myplugin() {
     register_widget('Youtube_Subs_Widget');
 }

 // Hook in function
 add_action('widgets_init', 'register_myplugin');

我希望有人知道出了什么问题并可以帮助我纠正我的错误,在此先感谢

4

1 回答 1

1

我尝试重新安装我的 WordPress 文件,然后它工作了

于 2020-12-21T03:18:40.420 回答