• lehar001

    (@lehar001)


    Hi!

    I’ve just begun fiddling with plugins and widgets trying to create my first theme for others to use.

    Im trying to create a drop down input where the admin can choose to auto play the video output by the widget or not.

    Default value is “Yes”, but if you change to “No” and update, it automatically resets to “Yes”. The value, “no” is however stored correctly so even though it says “Yes” the field outputs “no” on the front end.

    What am I doing wrong here?

    // Creating the widget
    class twitch_widget extends WP_Widget {
    
    function __construct() {
    parent::__construct(
    // Base ID of your widget
    'twitch_widget', 
    
    // Widget name will appear in UI
    __('Twitch streams', 'twitch_widget_domain'), 
    
    // Widget description
    array( 'description' => __( 'This widget outputs Twitch streams', 'twitch_widget_domain' ), )
    );
    }
    
    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
    $title = apply_filters( 'widget_title', $instance['title'] );
    $autoplay = $instance['autoplay'];
    
    // before and after widget arguments are defined by themes
    echo $args['before_widget'];
    if ( ! empty( $title ) )
    echo $args['before_title'] . $title . $args['after_title'];
    
    // This is where you run the code and display the output
    
    // The Query
    
    $args = array( 'post_type' => 'streams', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    
    // The Loop
    if ( $loop->have_posts() ) {
    	while ( $loop->have_posts() ) {
    		$loop->the_post();
    
    // Get variable data 
    
    $twitchusername = get_field('twitchusername');
    
    	if ( $autoplay == 'Yes' ) {
    		$autoplay_value = 'true';
    		} else {
    		$autoplay_value = 'false';
    	}
    
    // Post content
    
    echo '<object bgcolor="#000000"
            data="//www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf"
            height="378"
            type="application/x-shockwave-flash"
            width="620"
            >
      <param name="allowFullScreen"
              value="true" />
      <param name="allowNetworking"
              value="all" />
      <param name="allowScriptAccess"
              value="always" />
      <param name="movie"
              value="//www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf" />
      <param name="flashvars"
              value="channel=' . $twitchusername . '&auto_play=' . $autoplay_value . '&start_volume=25" />
    </object>';
    
    echo $autoplay;
    
    	}
    
    } else {
    	// no posts found
    }
    
    /* Restore original Post Data */
    wp_reset_postdata();
    
    //End of loop
    
    echo $args['after_widget'];
    }
    
    // Widget Backend
    public function form( $instance ) {
    if ( isset( $instance[ 'title' ] ) ) {
    $title = $instance[ 'title' ];
    $select = esc_attr($instance['autoplay']);
    
    }
    else {
    $title = __( 'New title', 'twitch_widget_domain' );
    $select = '';
    }
    
    // Widget admin form
    ?>
    <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('autoplay'); ?>"><?php _e('Autoplay streams?', 'wp_widget_plugin'); ?></label>
    <select name="<?php echo $this->get_field_name('autoplay'); ?>" id="<?php echo $this->get_field_id('autoplay'); ?>" class="widefat">
    <?php
    $options = array('Yes', 'No');
    foreach ($options as $option) {
    echo '<option value="' . $option . '" id="' . $option . '"', $autoplay == $option ? ' selected="selected"' : '', '>', $option, '</option>';
    }
    ?>
    </select>
    
    </p>
    
    <?php
    }
    
    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
    $instance = array();
    
    // Fields
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
    $instance['autoplay'] = strip_tags($new_instance['autoplay']);
    
    return $instance;
    }
    }
  • The topic ‘Widget backend drop down reseting to default value’ is closed to new replies.