I have a problem in a wirdpress widget:
When i save the (edited) widget data in wp backend, the widget fields (of only this widget) are empty on the automatically loaded (same) page. If i refreh the browser then, the fields contain the new values.
In the frontent, the widget does never display anything ($instance['title']
and $instance['category']
are empty.
<?php
class ranger_calendar_widget_list extends WP_Widget {
protected $calendar;
protected $months;
public function __construct() {
parent::__construct('',
__('Calendar', 'ranger-calendar'),
array(
'classname' => 'ranger_calendar_widget_list',
'description' => __('Displays the calendar as a list.', 'ranger-calendar'),
));
$this -> calendar = new ranger_calendar();
$this -> months = array(
1 => __('January'),
2 => __('February'),
3 => __('March'),
4 => __('April'),
5 => __('May'),
6 => __('June'),
7 => __('July'),
8 => __('August'),
9 => __('September'),
10 => __('October'),
11 => __('November'),
12 => __('Decenber')
);
}
/**
* Displays the widget's content
*/
function widget($args, $instance) {
$title = apply_filters('widget_title', $instance['title']);
$category = $instance['category'];
echo $args['before_widget'];
echo $args['before_title'] . $instance['title'] . $args['after_title'];
print_r($instance);
if (!$category) {
$category = 0;
}
$dates = $this -> calendar -> get_future_dates();
$dates_final = array();
if ($category != 0) {
foreach ($dates as $date) {
if ($date['category'] == $category) {
$dates_final[] = $date;
}
}
} else {
$dates_final = $dates;
}
echo $this -> generate_html($dates);
echo $args['after_widget'];
}
public function generate_html($dates) {
$html = "<ol class="date-list">";
foreach ($dates as $date) {
$id = $date['id'];
$beginning = $date['beginning'];
$datetime = date_create($beginning);
$title = $date['title'];
$html .= "<li>";
$html .= "<time datetime="" . $beginning . "">";
$html .= "<span class="month">" . $this->months[intval(date_format($datetime, "m"))] . "</span>";
$html .= "<span class="day">" . date_format($datetime, "d") . "</span>";
$html .= "</time>";
$html .= $title;
$html .= "</li>";
}
$html .= "</ol>";
return $html;
}
public function form($instance) {
// Title field
if (isset($instance['title'])) {
$title = $instance['title'];
} else {
$title = __('Dates', 'ranger-calendar');
}
echo "<p>";
echo "<label for='" . $this -> get_field_id('title');
echo "'>" . __('Title', 'ranger-calendar') . "</label> ";
echo "<input type='text' id='" . $this -> get_field_id('title');
echo "' name='" . $this -> get_field_id('title');
echo "' value='" . esc_attr($title) . "' />";
echo "</p>";
// Category field
if (isset($instance['category'])) {
$category = $instance['category'];
} else {
$category = 0;
}
$categories = $this -> calendar -> get_categories();
echo "<p>";
echo "<label for='" . $this -> get_field_id('category');
echo "'>" . __('Category', 'ranger-calendar') . "</label> ";
echo "<select name='" . $this -> get_field_id('category');
echo "' id='" . $this -> get_field_id('category');
echo "'>";
echo "<option value='0'";
if($instance['category'] == '0') {
echo " selected";
}
echo ">" . __('None', 'ranger-calendar') . "</option>";
foreach($categories as $category) {
echo "<option value='" . $category['id'] . "'";
if($category['id'] == $instance['category']) {
echo " selected";
}
echo ">" . $category['name'] . "</option>";
}
echo "</select>";
echo "</p>";
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
$instance['category'] = (!empty($new_instance['category'])) ? strip_tags($new_instance['category']) : '';
return $instance;
}
}
add_action('widgets_init', 'register_ranger_calendar_widget_list');
function register_ranger_calendar_widget_list() {
register_widget('ranger_calendar_widget_list');
}
Read more here: WordPress widget admin form updates values too late