Title: Widget &#8211; get array value in $widget_ops
Last modified: August 19, 2016

---

# Widget – get array value in $widget_ops

 *  [artfulalibi](https://wordpress.org/support/users/artfulalibi/)
 * (@artfulalibi)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/widget-get-array-value-in-widget_ops/)
 * 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); ?> />&nbsp;<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');
       ```
   

Viewing 1 replies (of 1 total)

 *  [Bananavice](https://wordpress.org/support/users/bananavice/)
 * (@bananavice)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/widget-get-array-value-in-widget_ops/#post-2023706)
 * This is a bit late, but this is exactly the same thing that I needed to do. I
   tried for a while to push the class into the widget options in different ways
   but failed with everything so I just went with a preg_replace on $before_widget.
 * This is how it looked for me:
 *     ```
       function widget($args, $instance) {
       	extract($args);
   
       	$color = $instance['color'];
       	$before_widget = preg_replace('/class="(.*?)"/i', 'class="$1 '.$color.'"', $before_widget);
   
       	echo $before_widget;
       		echo '<h3>'.$instance['title'].'</h3>';
       		echo '<div class="text">'.$instance['text'].'</div>';
       	echo $after_widget;
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Widget – get array value in $widget_ops’ is closed to new replies.

## Tags

 * [class](https://wordpress.org/support/topic-tag/class/)
 * [select](https://wordpress.org/support/topic-tag/select/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * 1 reply
 * 2 participants
 * Last reply from: [Bananavice](https://wordpress.org/support/users/bananavice/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/widget-get-array-value-in-widget_ops/#post-2023706)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
