我创建了一个自定义窗口小部件,但如果点击“保存”,它就无法保存。
class Whistle_Text_Widget extends WP_Widget{
public function __construct(){
$widget_options = array(
'classname' => 'test_widget',
'description' => 'This is a simple text widget'
);
parent::__construct('text_widget', 'Whistle Text Widget', $widget_options);
}
public function form($instance){
if(!empty($instance)){
$title = $instance['title'];
$description = $instance['description'];
}
else{
$title = '';
$description = '';
}
?>
<p>
<label for="<?php $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label><br />
<input type="text" id="<?php $this->get_field_id('title'); ?>" name="<?php $this->get_field_name('title'); ?>" class="text-input" value="<?php echo $title; ?>">
</p>
<p>
<label for="<?php $this->get_field_id('description'); ?>"><?php _e('Description:'); ?></label><br />
<input type="text" id="<?php $this->get_field_id('description'); ?>" name="<?php $this->get_field_name('description'); ?>" class="text-input" value="<?php echo $description; ?>">
</p>
<?php
}
public function widget($args, $instance){
echo $instance['title'];
echo $instance['description'];
}
public function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['description'] = $new_instance['description'];
return $instance;
}
}
function whistle_text_widget(){
register_widget('Whistle_Text_Widget');
}
add_action('widgets_init', 'whistle_text_widget');
答案 0 :(得分:0)
试试这段代码。你忘了回复id
和input field name
。
class Whistle_Text_Widget extends WP_Widget{
public function __construct(){
$widget_options = array(
'classname' => 'test_widget',
'description' => 'This is a simple text widget'
);
parent::__construct('text_widget', 'Whistle Text Widget', $widget_options);
}
public function widget($args, $instance){
echo $instance['title'];
echo $instance['description'];
}
public function form($instance){
if(!empty($instance)){
$title = $instance['title'];
$description = $instance['description'];
}
else{
$title = '';
$description = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label><br />
<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" class="text-input" value="<?php echo $title; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Description:'); ?></label><br />
<input type="text" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" class="text-input" value="<?php echo $description; ?>">
</p>
<?php
}
public function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['description'] = $new_instance['description'];
return $instance;
}
}
function whistle_text_widget(){
register_widget('Whistle_Text_Widget');
}
add_action('widgets_init', 'whistle_text_widget');