0

I am trying to save the value from a dropdown box on a custom widget created by me. the title from the same widget is getting saved, but not the values from drop down box. Can someone please help me? Here is the code:

    <?php // Creating the widget 
class rspwidget extends WP_Widget {

    function __construct() {
    parent::__construct(
    // Base ID of your widget
    'rspwidget', 

    // Widget name will appear in UI
    __('RSP Ad Widget', 'rspwidget_ad'), 

    // Widget description
    array( 'description' => __( 'Revenue share Plugin(RSP) Widget', 'rspwidget_ad' ), ) 
        );
    }

    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
        echo $args['before_widget'];
        if ( ! empty( $instance['title'] ) ) {
            echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
        }
        // before and after widget arguments are defined by themes

        // This is where you run the code and display the output
        //echo __( 'Hello, World!', 'rspwidget_ad' );
        echo adsensewidgetad();
        echo $args['after_widget'];
    }

    // Widget Backend 
    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : __( '', 'rspwidget_ad' );
        $widgetads = ! empty( $instance['$widgetads'] ) ? $instance['$widgetads'] : __( '', 'rspwidget_ad' );
    // Widget admin form
?>
    <p>
        <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 ); ?>" />

        <label for="<?php echo $this->get_field_id( 'widgetads' ); ?>"><?php _e( 'Ad dimensions:', 'widgetads' ); ?></label>
        <select id="<?php echo $this->get_field_id( 'widgetads' ); ?>" name="<?php echo $this->get_field_name( 'widgetads' ); ?>" type="text">
            <option value="300x250">300x250px</option>
            <option value="336x280">336x280px</option>
            <option value="300x600">300x600px</option>
            <option value="320x100">320x100px</option>
        </select>
    </p>
<?php 
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
            $instance = array();
            //$instance = $old_instance;
            $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
            $instance['widgetads'] = ( ! empty( $new_instance['widgetads'] ) ) ? strip_tags( $new_instance['widgetads'] ) : '';
            return $instance;
    }
} // Class rspwidget ends here

//create the ad for the widget
function adsensewidgetad() {  
            $options = get_option('RSP_options');
            if (!get_option('RSP_options')) {
                return 'Configure RSP settings!';
            }
            $position = '';
            if (array_key_exists('radio_option1', $options)) {
                $position =  $options['radio_option1'];
            }
            /*if (array_key_exists('radio_option2', $options)) {
            $position =  $options['radio_option2'];
            }
            if (array_key_exists('radio_option3', $options)) {
            $position =  $options['radio_option3'];
            }*/
            if(get_the_author_meta( 'RSP_text_string' )){
                $input = array($options['RSP_text_string'], get_the_author_meta( 'RSP_text_string' ));
            } else {
                $input = array($options['RSP_text_string']);
            }
            shuffle($input);

            if ($input[0] == 'pub-0000') {
                return 'Configure PUB ID!';
            }
        $ad_content = '<div align=center><script type="text/javascript"><!--  
google_ad_client = "ca-'.$input[0].'";  
google_ad_width = 300;  
google_ad_height = 250;  
//-->
</script>  
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>';
        if($position == 'Top') {
            return $ad_content.'<br />'.$content;
        }
        if($position == 'Bottom') {
            return $content.'<br />'.$ad_content;
        }
        if($position == 'Middle') {
            $count_words = strlen($content)/2;
            $insert_ad = strpos($content, '. ', $count_words);
            $ad_content = '<br />'.$ad_content.'<br />';
            return substr_replace($content, $ad_content, $insert_ad+2, 0);
        }
            return $ad_content;
    }

// Register and load the widget
function rsp_load_widget() {
    register_widget( 'rspwidget' );
}
add_action( 'widgets_init', 'rsp_load_widget' );
?>
1
  • 2
    In function form you write $instance['$widgetads'] is not $instance['widgetads']?
    – jep
    Commented Dec 30, 2014 at 9:24

1 Answer 1

2

Values are saved, but then you have to select the correct option when you print the select. Is the same when you print the input field and you add
value="<?php echo esc_attr( $title ); ?>".

You can try to change the code between <select> and </select> with this

<option value="300x250" <?php echo "300x250" == $widgetads ? "selected" : ""; ?> >300x250px</option>
<option value="336x280" <?php echo "336x280" == $widgetads ? "selected" : ""; ?>>336x280px</option>
<option value="300x600" <?php echo "300x600" == $widgetads ? "selected" : ""; ?>>300x600px</option>
<option value="320x100" <?php echo "320x100" == $widgetads ? "selected" : ""; ?>>320x100px</option>
1
  • I had to do according to the comment posted by you to the question as well to make it work. Thanks a lot.
    – defiant
    Commented Dec 31, 2014 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.