I'm trying to add a replica of the default WP text widget, with my own css class parameter, to functions.php so that it appears in the widgets manager and can be added to a sidebar.
My first attempt is below, but I'm certain there has to be an easier way than how I'm doing it.
Can this be done in a simpler manner?
In functions.php, I've got this...
$google_search = TEMPLATEPATH . "/google_search.php";require_once($google_search);
add_action('widgets_init', create_function('', "register_widget('My_Widget_Search');"));
In google_search.php, I've got... (Everything works except the textarea field contents aren't being saved)
<?php
class My_Widget_Search extends WP_Widget {
function My_Widget_Search() {
$widget_ops = array( 'classname' => 'widget_search', 'description' => __( "Google Adsense Search Widget Placeholder" ) );
$this->WP_Widget('adsense_search', __('Adsense Search Widget'), $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( '' ) : $instance['title']);
$text = apply_filters('widget_text', empty( $instance['text'] ) ? __( '' ) : $instance['text']);
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['text'] = $new_instance['text'];
return $instance;
}
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = esc_attr( $instance['title'] );
$text = $instance['text'];
?>
<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 $title; ?>" /></p>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name=""<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
<?php
}
}