新手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;
}
非常感谢!
答案 0 :(得分:0)
在你的函数更新中尝试使用$ instance作为空白数组而不是$ old_instance并检查一次像..
function update( $new_instance, $old_instance ) {
$instance = array();
.........
}
答案 1 :(得分:0)
感谢您的帮助!修复了我的问题,但是在表单代码中:我没有使用适当的Wordpress方法来识别id,name&amp;输入元素的值:
我有这个:
<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&amp;中进行硬编码。名字,现在它完全保存。
谢谢!