单击保存时,我的小部件不会更新其值

时间:2013-09-05 作者:user2619611

因此,这是我第一次尝试创建一个小部件,它具有创建简单图像滚动效果的功能(如果任何人在完成后想要它,请给我留言)。

现在我下载了一个示例小部件并进行了一些更改,但我仍然没有触及输出的html部分,所以请忽略该部分。现在我的问题是,当我单击“保存”时,字段中的值会返回到其默认值,而在示例小部件上没有这样做。这是我的代码:

<?php
/**
 * Plugin Image: Rollover Widget
 * Description: A widget that creates a rollover image effect with a hyperlink.
 * Version: 0.1
 * Author: Zak Elas
 * Author URI:
 */


add_action( \'widgets_init\', \'my_widget\' );


function my_widget() {
    register_widget( \'MY_Widget\' );
}

class MY_Widget extends WP_Widget {

    function MY_Widget() {
        $widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that creates a rollover image effect with a hyperlink. \', \'example\') );

        $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'Rollover Widget\' );

        $this->WP_Widget( \'example-widget\', __(\'Example Widget\', \'example\'), $widget_ops, $control_ops );
    }

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

        //Our variables from the widget settings.
        $link = apply_filters(\'widget_link\', $instance[\'link\'] );
        $image = $instance[\'image\'];
        $rollover_image = $instance[\'rollover_image\'];

        echo $before_widget;

        // Display the widget link 
        if ( $link )
            echo $before_link . $link . $after_link;

        //Display the name 

            printf( \'<p>\' . __(\'Hey their Sailor! My name is %1$s.\', \'example\') . \'</p>\', $image );


            printf( \'<p>\' . __(\'Hey their Sailor! My name is %1$s.\', \'example\') . \'</p>\', $rollover_image );



        echo $after_widget;
    }

    //Update the widget 

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

        //Strip tags from link and name to remove HTML 
        $instance[\'link\'] = strip_tags( $new_instance[\'link\'] );
        $instance[\'image\'] = strip_tags( $new_instance[\'image\'] );
        $instance[\'rollover_image\'] = strip_tags( $new_instance[\'rollover_image\'] );

        return $instance;
    }


    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( \'link\' => __(\'Example\', \'example\'), \'image\' => __(\'/images/editorial.png\', \'example\') , \'rollover_image\' => __(\'/images/editorial.png\', \'example\') );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>


        <p>
            <label for="<?php echo $this->get_field_id( \'link\' ); ?>"><?php _e(\'link\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'link\' ); ?>" name="<?php echo $this->get_field_name( \'link\' ); ?>" value="<?php echo $instance[\'link\']; ?>" style="width:100%;" />
        </p>


        <p>
            <label for="<?php echo $this->get_field_id( \'image\' ); ?>"><?php _e(\'image\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'image\' ); ?>" name="<?php echo $this->get_field_name( \'image\' ); ?>" value="<?php echo $instance[\'image\']; ?>" style="width:100%;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( \'rollover_image\' ); ?>"><?php _e(\'rollover_image:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'rollover_image\' ); ?>" name="<?php echo $this->get_field_name( \'image\' ); ?>" value="<?php echo $instance[\'rollover_image\']; ?>" style="width:100%;" />
        </p>



    <?php
    }
}

?>
下面是示例小部件代码

<?php
/**
 * Plugin Name: A simple Widget
 * Description: A widget that displays authors name.
 * Version: 0.1
 * Author: Bilal Shaheen
 * Author URI: http://gearaffiti.com/about
 */


add_action( \'widgets_init\', \'my_widget\' );


function my_widget() {
    register_widget( \'MY_Widget\' );
}

class MY_Widget extends WP_Widget {

    function MY_Widget() {
        $widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that displays the authors name \', \'example\') );

        $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'example-widget\' );

        $this->WP_Widget( \'example-widget\', __(\'Example Widget\', \'example\'), $widget_ops, $control_ops );
    }

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

        //Our variables from the widget settings.
        $title = apply_filters(\'widget_title\', $instance[\'title\'] );
        $name = $instance[\'name\'];
        $show_info = isset( $instance[\'show_info\'] ) ? $instance[\'show_info\'] : false;

        echo $before_widget;

        // Display the widget title 
        if ( $title )
            echo $before_title . $title . $after_title;

        //Display the name 
        if ( $name )
            printf( \'<p>\' . __(\'Hey their Sailor! My name is %1$s.\', \'example\') . \'</p>\', $name );


        if ( $show_info )
            printf( $name );


        echo $after_widget;
    }

    //Update the widget 

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

        //Strip tags from title and name to remove HTML 
        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'name\'] = strip_tags( $new_instance[\'name\'] );
        $instance[\'show_info\'] = $new_instance[\'show_info\'];

        return $instance;
    }


    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( \'title\' => __(\'Example\', \'example\'), \'name\' => __(\'Bilal Shaheen\', \'example\'), \'show_info\' => true );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        //Widget Title: Text Input.
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:100%;" />
        </p>

        //Text Input.
        <p>
            <label for="<?php echo $this->get_field_id( \'name\' ); ?>"><?php _e(\'Your Name:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'name\' ); ?>" name="<?php echo $this->get_field_name( \'name\' ); ?>" value="<?php echo $instance[\'name\']; ?>" style="width:100%;" />
        </p>


        //Checkbox.
        <p>
            <input class="checkbox" type="checkbox" <?php checked( $instance[\'show_info\'], true ); ?> id="<?php echo $this->get_field_id( \'show_info\' ); ?>" name="<?php echo $this->get_field_name( \'show_info\' ); ?>" /> 
            <label for="<?php echo $this->get_field_id( \'show_info\' ); ?>"><?php _e(\'Display info publicly?\', \'example\'); ?></label>
        </p>

    <?php
    }
}

?>

1 个回复
最合适的回答,由SO网友:Faishal 整理而成

在里面$control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'example-widget\' ); id\\u base应该没有空格,并且在滚动图像中,您的名称错误,您的名称设置为echo $this->get_field_name( \'image\' ); 应该是什么时候echo $this->get_field_name( \'rollover_image\' );

add_action ( \'widgets_init\' , \'my_widget\' ) ;

function my_widget () {
    register_widget ( \'MY_Widget\' ) ;
}

class MY_Widget extends WP_Widget {

    function MY_Widget () {
        $widget_ops = array ( \'classname\'   => \'example\' , \'description\' => __ ( \'A widget that creates a rollover image effect with a hyperlink. \' , \'example\' ) ) ;

        $control_ops = array ( \'width\'  => 300 , \'height\' => 350 ) ;

        $this -> WP_Widget ( \'example-widget\' , __ ( \'Example Widget\' , \'example\' ) , $widget_ops , $control_ops ) ;
    }

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

        //Our variables from the widget settings.
        $link           = apply_filters ( \'widget_link\' , $instance[ \'link\' ] ) ;
        $image          = $instance[ \'image\' ] ;
        $rollover_image = $instance[ \'rollover_image\' ] ;

        echo $before_widget ;

        // Display the widget link
        if ( $link )
            echo $before_link . $link . $after_link ;

        //Display the name

        printf ( \'<p>\' . __ ( \'Hey their Sailor! My name is %1$s.\' , \'example\' ) . \'</p>\' , $image ) ;


        printf ( \'<p>\' . __ ( \'Hey their Sailor! My name is %1$s.\' , \'example\' ) . \'</p>\' , $rollover_image ) ;



        echo $after_widget ;
    }

    //Update the widget

    function update ( $new_instance , $old_instance ) {

        $instance = $old_instance ;

        //Strip tags from link and name to remove HTML
        $instance[ \'link\' ]           = strip_tags ( $new_instance[ \'link\' ] ) ;
        $instance[ \'image\' ]          = strip_tags ( $new_instance[ \'image\' ] ) ;
        $instance[ \'rollover_image\' ] = strip_tags ( $new_instance[ \'rollover_image\' ] ) ;

        return $instance ;
    }

    function form ( $instance ) {

        //Set up some default widget settings.
        $defaults = array ( \'link\'           => \'Example\' , \'image\'          => \'/images/editorial.png\' , \'rollover_image\' => \'/images/editorial.png\' ) ;
        $instance = wp_parse_args ( ( array ) $instance , $defaults ) ;
        ?>


        <p>
            <label for="<?php echo $this -> get_field_id ( \'link\' ) ; ?>"><?php _e ( \'link\' , \'example\' ) ; ?></label>
            <input id="<?php echo $this -> get_field_id ( \'link\' ) ; ?>" name="<?php echo $this -> get_field_name ( \'link\' ) ; ?>" value="<?php echo $instance[ \'link\' ] ; ?>" style="width:100%;" />
        </p>


        <p>
            <label for="<?php echo $this -> get_field_id ( \'image\' ) ; ?>"><?php _e ( \'image\' , \'example\' ) ; ?></label>
            <input id="<?php echo $this -> get_field_id ( \'image\' ) ; ?>" name="<?php echo $this -> get_field_name ( \'image\' ) ; ?>" value="<?php echo $instance[ \'image\' ] ; ?>" style="width:100%;" />
        </p>

        <p>
            <label for="<?php echo $this -> get_field_id ( \'rollover_image\' ) ; ?>"><?php _e ( \'rollover_image:\' , \'example\' ) ; ?></label>
            <input id="<?php echo $this -> get_field_id ( \'rollover_image\' ) ; ?>" name="<?php echo $this -> get_field_name ( \'rollover_image\' ) ; ?>" value="<?php echo $instance[ \'rollover_image\' ] ; ?>" style="width:100%;" />
        </p>



        <?php
    }

}

结束

相关推荐

如何禁用我的主机强制执行的插件(在u-plugins中)?

我的主机制作了两个必需的插件,它们会在mu plugins文件夹下自动更新。有没有一种方法可以通过一些技巧来禁用它们,例如在我的本地插件文件夹下使用一个同名的插件文件夹,或者复制一些函数并使其为空?