I've been working on my first widget, Everything works perfectly except it doesn't save anything I add to the textarea box. It will output the input on the website however.
From research I think its something to do with the form function and update, but can't quite get my head around which part.
Here is my widget code.
<?php
/*
Plugin Name: Hexagran Featured Widget
Plugin URI: http://thevisionists.com
Description: Produces a widget for use in the widget panel.
Version: 0.1
Author: Danny & Tom
Author URI: http://thevisionists.com
License:none
*/
//$args pulls in the arguments from the sidebar.php
class hexagram_featured_widget extends WP_Widget {
function __construct() {
parent::__construct('false', $name = __( 'Hexagram Featured Box'), //tranlation enabled // accessing the perant constructer
array( 'description' => __('Displays the content you place inside, strips all html'))
); // end of the construct
}
function form($instance) {
$title = strip_tags( $instance['title'] );
$info = esc_textarea( $instance['info'] );
?>
<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>
<p><label for="<?php echo $this->get_field_id('info'); ?>"><?php _e('Text Goes here:'); ?></label>
<textarea class="widefat" rows="8" cols="10" id="<?php echo $this->get_field_id('info'); ?>" name="<?php echo $this->get_field_name('info'); ?>"></textarea>
<?php
}
function update( $new_instance, $old_instance) {
$instance = array();
$instance[ 'title' ] = strip_tags($new_instance[ 'title' ]);
$instance[ 'info' ] = strip_tags($new_instance[ 'info' ]);
return $instance;
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
$info = $instance[ 'info' ];
echo $before_widget;
if ( ! empty( $title ) ) echo $before_title . $title . $after_title;
if ($info) print $info;
echo $after_widget;
}
} // end of the class holding all the functions
add_action('widgets_init', function() {
register_widget('hexagram_featured_widget');
}
)
// adds an action to register the widget
// class name is always the name of the register widget argument.
?>