So my widget works. You can select a color in the admin, and everything is saved correctly to $instance['color']. But I can't seem to add it to the processing function, so that I append the class with the chosen color value. I am sure I am missing something basic here, and I ll feel dumb in the end. Any help is appreciated greatly.
class OOS_Widget_ColorText extends WP_Widget {
// widget actual processes
function OOS_Widget_ColorText($instance) {
$color = $instance['color'];
$widget_ops = array('classname' => array('facebook_link', "$color"), 'description' => __('Text widget with color option'));
$control_ops = array('width' => 400, 'height' => 350);
$this->WP_Widget('color_text', 'Color Text', $widget_ops, $control_ops);
}
// outputs the content of the widget
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$text = apply_filters( 'widget_text', $instance['text'], $instance );
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
<?php
echo $after_widget;
}
// processes widget options to be saved
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if ( current_user_can('unfiltered_html') )
$instance['text'] = $new_instance['text'];
else
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
$instance['filter'] = isset($new_instance['filter']);
if ( in_array( $new_instance['color'], array( 'pink', 'blue', 'green' ) ) ) {
$instance['color'] = $new_instance['color'];
} else {
$instance['color'] = 'pink';
}
return $instance;
}
// outputs the options form on admin
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$title = strip_tags($instance['title']);
$text = esc_textarea($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 esc_attr($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>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<p>Pick the box color for home page, or text color for other pages.</p>
<p>
<label>Color Options</label>
<select name="<?php echo $this->get_field_name('color'); ?>" id="<?php echo $this->get_field_id('color'); ?>">
<option value="pink"<?php selected( $instance['color'], 'pink' ); ?>><?php _e('pink'); ?></option>
<option value="blue"<?php selected( $instance['color'], 'blue' ); ?>><?php _e('blue'); ?></option>
<option value="green"<?php selected( $instance['color'], 'green' ); ?>><?php _e( 'green' ); ?></option>
</select>
</p>
<?php
}
}
register_widget('OOS_Widget_ColorText');