WordPress Custom Widget Select Options Not Saving

I’m very new to coding WordPress plugins/widgets (this is my first time!). I’ve built a very basic WordPress plugin which consists of 2 text inputs and a select field. The text inputs work fine however the select box doesn’t appear to be saving when I hit the “Save” button.

Here is my plugin code:

Read More
<?php
/* Plugin Name: Sidebar Box
Plugin URI: http://www.website.com
Description: Displays contact box in sidebar
Version: 1.0
Author: JM
Author URI: N/A
*/

// use widgets_init action hook to execute custom function
add_action( 'widgets_init', 'jm_box_widget' );

 //register our widget
function jm_box_widget() {
    register_widget( 'jm_box_widget_my_info' );
}

//boj_widget_my_info class
class jm_box_widget_my_info extends WP_Widget {

    //process the new widget
    function jm_box_widget_my_info() {
        $widget_ops = array( 
            'classname' => 'jm_box_widget_class', 
            'description' => 'Sidebar Box Widget.'
            ); 
        $this->WP_Widget( 'jm_box_widget_my_info', 'Box Widget', $widget_ops );
    }

     //build the widget settings form
    function form($instance) {
        $defaults = array( 'title' => 'Box Page Widget', 'description' => '', 'boxtype' => '' ); 
        $instance = wp_parse_args( (array) $instance, $defaults );
        $title = $instance['title'];
        $description = $instance['description'];
        $boxtype = $instance['boxtype'];

        ?>
            <p>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>"  type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
            <p>Description: <textarea class="widefat" name="<?php echo $this->get_field_name( 'description' ); ?>" / ><?php echo esc_attr( $description ); ?></textarea></p>
    <p>Sex:
            <select id="<?php echo $this->get_field_id( 'boxtype' ); ?>" name="<?php echo $this->get_field_name( 'boxtype' ); ?>" class="widefat" style="width:100%;">
            <option <?php if ( 'box1' == $instance['format'] ) echo 'selected="selected"'; ?> value="box1">box1</option>
    <option <?php if ( 'box2' == $instance['format'] ) echo 'selected="selected"'; ?> value="box2">box2</option>
            </select>
        </p>
        <?php
    }

    //save the widget settings
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['description'] = strip_tags( $new_instance['description'] );
        $instance['boxtype'] = ( $new_instance['boxtype'] ); 
        return $instance;
    }

    //display the widget
    function widget($args, $instance) {
        extract($args);

        echo $before_widget;
        $title = apply_filters( 'widget_title', $instance['title'] );
        $description = empty( $instance['description'] ) ? '&nbsp;' : $instance['description']; 
        $boxtype = empty( $instance['boxtype'] ) ? '&nbsp;' : $instance['boxtype']; 

        echo '<div class="sidebar-box" id="' . $boxtype . '" onmouseover="this.style.cursor='pointer'" onmouseup="window.location='' . $boxtype . ''">
                <h3>' . $title . '</h3>
                <p>' . $description . '</p>
                </div>';
        echo $after_widget;
    }
}

?>

I can’t for the life of me workout why it’s not saving.

Any help would be greatly appreciated.

Thanks,

James

Related posts

Leave a Reply

1 comment

  1. Somewhere along the line you must have changed $instance[‘format’] to $instance[‘boxtype’] but not in the form. The options need changing.

    <option <?php if ('box1' == $boxtype )