0

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

        ?>

2 Answers 2

2

there no "id_base" key in $control_ops; from what I know it only accepts width/height. move it

the id_base is the first argument you have there in WP_Widget()

1
  • this solved my issue in 2015! Apparently I was using outdated code as my custom widget template
    – Ejaz
    Commented Jan 28, 2015 at 10:32
0

the ID of sidebar (in php function) should not contain upparcase char (smsForm-widget)) !!!

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.