- Navigate to the WordPress plugin directory of your development installation.
- Navigate to the ch10-book-review-widget directory and edit ch10-book-review-widget.php.
- Find the Book_Reviews class and add the following block of code within the class to define the update method:
function update( $new_instance, $instance ) {
// Only allow numeric values
if ( is_numeric ( $new_instance['nb_book_reviews'] ) ) {
$instance['nb_book_reviews'] =
intval( $new_instance['nb_book_reviews'] );
} else {
$instance['nb_book_reviews'] =
$instance['nb_book_reviews'];
}
$instance['widget_title'] =
sanitize_text_field( $new_instance['widget_title'] );
$instance['render_widget'] =
sanitize_text_field( $new_instance['render_widget'] );
return $instance;
}
- Save and close the plugin file.
- Visit the Widgets section of the Appearance administration page and expand the Book Reviews widget instance.
- Enter a textual value in the Number of reviews to display field and save the widget. You will see that the field's value reverts to the last valid number saved for this field.