我的自定义小部件不保存在侧边栏中

时间:2011-01-25 作者:Elitmiar

我创建了一个自定义小部件,但如果单击“保存”,它不会保存。当我刷新页面时,它不会被保存。我的代码如下所示

     <?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 个回复
最合适的回答,由SO网友:onetrickpony 整理而成

没有“id\\U base”键$control_ops; 据我所知,它只接受宽度/高度。快

id\\u base是其中的第一个参数WP_Widget()

SO网友:T.Todua

提要栏的ID(在php函数中)不应包含upparcase char(smsForm-widget)) !!!

结束

相关推荐

Why use widgets?

我对使用WordPress很陌生,我想知道使用小部件的好处是什么?看here 这听起来像是为那些不是程序员的人准备的,他们想在他们的网站上添加插件。对吗?或者小部件是否在某种程度上使站点更加健壮?