0

新手 Wordpress 开发人员在这里,所以我真的可以使用一些帮助。我正在构建一个显示功能的小部件,但选项没有保存。当您点击“保存”时,所有框再次变为空白。我想我错过了一些明显的东西!

这是我的小部件代码的相关部分:

// First create the widget for the admin panel
class icon_features_widget extends WP_Widget {

  function icon_features_widget() {
    $widget_ops = array( 'description' => __( 'Displays icon features in a widget', 'icon-feature-widget' ) );
    $this->WP_Widget( 'icon_feature_widget', __( 'Icon Feature', 'icon-feature-widget' ), $widget_ops );
  }

  function form( $instance ) {

    $icon_feature_title = isset( $instance['icon_feature_title'] ) ? $instance['icon_feature_title'] : '';
    $icon_feature_text = isset( $instance['icon_feature_text'] ) ? $instance['icon_feature_text'] : '';
    $icon_feature_link_url = isset( $instance['icon_feature_link_url'] ) ? $instance['icon_feature_link_url'] : '';
    $icon_feature_link_text = isset( $instance['icon_feature_link_text'] ) ? $instance['icon_feature_link_text'] : '';
    $radioValue = isset( $instance['radioValue'] ) ? $instance['radioValue'] : '';
    $icon_feature_icon_class = isset( $instance['icon_feature_icon_class'] ) ? $instance['icon_feature_icon_class'] : '';
    $icon_feature_custom_icon_url = isset( $instance['icon_feature_custom_icon_url'] ) ? $instance['icon_feature_custom_icon_url'] : '';
    ?>

    <p>
      <label for="icon_feature_title">Title:</label><br />
      <input type="text" name="icon_feature_title" id="icon_feature_title" value="<?php echo $icon_feature_title ; ?>" />
    </p>

    <p>
      <label for="icon_feature_text">Text:</label><br />
      <textarea name="icon_feature_text" id="icon_feature_text" value="<?php echo $icon_feature_text ; ?>" rows="6" cols="21"/></textarea>
    </p>

    <p>
      <label for="icon_feature_link_url">Link URL:</label><br />
      <input type="text" name="icon_feature_link_url" id="icon_feature_link_url" value="<?php echo $icon_feature_link_url ; ?>" />
    </p>

    <p>
      <label for="icon_feature_link_text">Link Text (defaults to "Read More"):</label><br />
      <input type="text" name="icon_feature_link_text" id="icon_feature_link_text" value="<?php echo $icon_feature_link_text ; ?>" />
    </p>

    <p>Choose which icon source you'd like to use.</p>
    <p>
      <label>
        <input type="radio" id="iconRadio" name="whichToDisplay" value="icon" <?php echo ($radioValue == 'icon' || $radioValue == '') ? 'checked="checked"':''; ?>/>
        Choose from included icons
      </label><br />
      <label>
        <input type="radio" id="customIconRadio" name="whichToDisplay" value="customIcon" <?php echo ($radioValue == 'customIcon') ? 'checked="checked"':''; ?>/>
        Display custom icon
      </label><br />
      <label>
        <input type="radio" id="nothingRadio" name="whichToDisplay" value="nothing" <?php echo ($radioValue == 'nothing') ? 'checked="checked"':''; ?>/>
        Display neither
      </label><br /><br />
    </p>

    <p> <!-- REPLACE THIS WITH SOME KIND OF ICON CHOOSER DROPDOWN THING -->
      <label for="icon_feature_icon_class">Icon Class:</label><br />
      <input type="text" name="icon_feature_icon_class" id="icon_feature_icon_class" value="<?php echo $icon_feature_icon_class ; ?>" />
    </p>

    <p> <!-- REPLACE THIS WITH A WORDPRESS IMAGE CHOOSER / MEDIA LIBRARY CHOOSER -->
      <label for="icon_feature_custom_icon_url">Custom Icon URL:</label><br />
      <input type="text" name="icon_feature_custom_icon_url" id="icon_feature_custom_icon_url" value="<?php echo $icon_feature_custom_icon_url ; ?>" />
    </p>

    <?php 
  }

  function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['icon_feature_title'] = $new_instance['icon_feature_title'];
    $instance['icon_feature_text'] = $new_instance['icon_feature_text'];
    $instance['icon_feature_link_url'] = $new_instance['icon_feature_link_url'];
    $instance['icon_feature_link_text'] = $new_instance['icon_feature_link_text'];
    $instance['radioValue'] = $new_instance['radioValue'];
    $instance['icon_feature_icon_class'] = $new_instance['icon_feature_icon_class'];
    $instance['icon_feature_custom_icon_url'] = $new_instance['icon_feature_custom_icon_url'];
    return $instance;
  }

非常感谢!

4

2 回答 2

0

谢谢您的帮助!不过修复了我的问题,这是在表单代码中:我没有使用正确的 Wordpress 方法来处理输入元素的 id、名称和值:

我有这个:

<input type="text" name="icon_feature_link_text" id="icon_feature_link_text" value="<?php echo $icon_feature_link_text ; ?>" />

我把它改成这样:

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

我对所有输入(以及标签)都这样做了,而不是在 id 和 name 中进行硬编码,现在它可以完美保存。

谢谢!

于 2013-04-08T20:00:21.970 回答
0

在您的函数更新中尝试使用 $instance 作为空白数组而不是 $old_instance 并检查一次类似..

function update( $new_instance, $old_instance ) {

        $instance = array();
.........
}
于 2013-04-08T06:17:51.567 回答