Reputation: 29
I created a custom widget, but it's not saving if I click on save.
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');
Upvotes: 0
Views: 136
Reputation: 9342
Try this code. you forgot to echo the id
and 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');
Upvotes: 1