My Custom Widget does not save in sidebar

Written by  on September 6, 2016 

I created a custom widget, but it’s not saving if I click on save. When I refresh the page it is not saved. My code looks like follows

     <?php
/**  * Plugin Name: Sending a
     Contact SMS  * Plugin URI:
     http://www.test.co.za  * Description:
     Allows you to send a contact SMS to
     website owner  * Author: test  *
     Version: 1.0  * Author URI:
     http://test.co.za
     */

     class sendSMS extends WP_Widget {

         function sendSMS() {
               $widget_ops = array(
                   'classname' => 'sendSMS',
                   'description' => 'Allows you to add a small contact form that will send out an sms to the user.',
               );
               $control_ops = array( 'id_base' => 'smsForm-widget' );
               $this->WP_Widget( 'smsForm-widget', 'Send SMS', $widget_ops, $control_ops);  }

         function form ($instance) {
             $defaults = array('title'=> __('Example', 'example'), 'name' =>  __('John Doe', 'example'), 'number' => __('+27825656994', 'example'), 'message' =>  __('Tell Us', 'example') );
             $instance = wp_parse_args( (array) $instance, $defaults );
             ?>

                <p>
                        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
                        <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
                </p>

                <p>
                        <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Your Name:', 'name'); ?></label>
                        <input id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo $instance['name']; ?>" />
                </p>

                <!-- Your Name: Text Input -->
                <p>
                        <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Your Cellphone Number:', 'number'); ?></label>
                        <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>"  />
                </p>

                <p>
                        <label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e('Your Message:', 'message'); ?></label>
                        <textarea id="<?php echo $this->get_field_id( 'message' ); ?>" name="<?php echo $this->get_field_name( 'message' ); ?>"><?php echo $instance['message']; ?></textarea>
                </p>


             <?php

         }

         function update ($new_instance, $old_instance) {
            $instance = $old_instance;

            /* Strip tags (if needed) and update the widget settings. */
            $instance['title'] = strip_tags( $new_instance['title'] );
            $instance['name'] = strip_tags( $new_instance['name'] );
            $instance['number'] = $new_instance['number'];
            $instance['message'] = $new_instance['message'];

            return $instance;

         }

         function widget ($args,$instance) {
            extract( $args );

            /* Our variables from the widget settings. */
            $title = apply_filters('widget_title', $instance['title'] );
            $name = $instance['name'];
            $number = $instance['number'];
            $message = $instance['message'];

            /* Before widget (defined by themes). */
            echo $before_widget;

            /* Display the widget title if one was input (before and after defined by themes). */
            if ( $title )
                    echo $before_title . $title . $after_title;

            /* Display name from widget settings if one was input. */
            if ( $name )
                    printf( '<p>' . __('Hello. My name is %1$s.', 'example') . '</p>', $name );

            if ( $number )
                    printf( '<p>' . __('Hello. My number is %1$s.', 'example') . '</p>', $name );

            if ( $message )
                    printf( '<p>' . __('Hello. My message is %1$s.', 'example') . '</p>', $message );

            /* After widget (defined by themes). */
            echo $after_widget;  }

        }

        function load_sms_form() {  register_widget( 'sendSMS' ); }

        add_action('widgets_init', 'load_sms_form');

         // register the widget

        ?>

Category : Wordpress

Tags :