I am working on a widget for my theme. If I do a
print_r($insance)
in the form function, the instance array is populated with the correct data. I have checked the database and the correct information is there in the options table for my widget.
My problem is that, if I do a
print_r($instance)
for the front end, The array is empty and I get undefined indexes in the widget function. I have not been able to figure out what is wrong.
class alliance_post_builder extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'alliance_post_builder', // Base ID
__( 'Alliance post builder', 'alliance' ), // Name
array( 'description' => __( 'A post builder', 'alliance' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract( $args );
$sectionID = $instance['sectionID'];
$title = $instance['title'];
$category = $instance['category'];
?><section id="<?php echo $sectionID; ?>"><?php
print_r($instance);
?></section><?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['sectionID'] = strip_tags( $new_instance['sectionID'] );
$instance['category'] = $new_instance['category'];
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$defaults = array( 'title' => __('A new section', 'alliance'), 'sectionID' => __('', 'alliance'), 'category' => -1 );
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id( 'sectionID' ); ?>"><?php _e( 'Section Id:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'sectionID' ); ?>" name="<?php echo $this->get_field_name( 'sectionID' ); ?>" type="text" value="<?php echo esc_attr( $sectionID ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
wp_dropdown_categories( array(
'name' => $this->get_field_name( 'category' ),
'selected' => $instance["category"],
'show_option_none' => 'None',
) );
}
}
get_categories
as it is better suited, but OK, as you said, it is irrelevant here. Also, I suggested that post as there are problems with your widgets, like for one, you should never useextract()