WordPress Widget未保存实例

时间:2015-08-05 作者:John Cargo

我有一个非常简单的wordpress小部件,它有3个输入字段。每当我提交数据时,它不会保存到数据库中。我已经添加和删除了插件,停用和重新激活了多次,但没有运气。下面是我的代码:-

    class price_package extends WP_Widget {

    function  __construct() {
        parent::__construct(
            \'price_package\',
            \'Price Package\',
            array(
                \'classname\' => \'price-package\',
                \'description\' => \'Wordpress Package Page Creator\'
            )
        );
        add_action(\'admin_print_styles\', array($this, \'register_admin_styles\'));
        add_action(\'wp_enqueue_scripts\', array($this, \'register_widget_styles\'));
        add_shortcode(\'package\', function($atts, $content = null) {
            $atts = shortcode_atts(
                array(
                    \'name\' => \'Super Platinum\',
                    \'content\' => \'No description available.\'
                ), $atts
            );
            extract($atts);
            include(plugin_dir_path(__FILE__).\'/views/package.php\');
        });
    }

    function form ($instance) {
        $instance = wp_parse_args(
            (array)$instance,
            array (
                \'name\' => \'\',
                \'divclass\'  => \'\',
                \'paypal\' => \'\',
            )
        );
        include(plugin_dir_path(__FILE__).\'/views/admin.php\');
    }

    function update($new_instance, $old_instance) {
        $old_instance[\'name\'] = htmlentities($new_instance[\'name\']);
        $old_instance[\'divclass\'] = htmlentities($new_instance[\'divclass\']);
        $old_instance[\'paypal\'] = htmlentities($new_instance[\'paypal\']);
        return $old_instance;
    }

    function widget($args, $instance) {
        extract($args, EXTR_SKIP);
        echo $before_widget;
        include(plugin_dir_path(__FILE__).\'/views/widget.php\');
        echo $after_widget;
    }

    function register_admin_styles() {
        wp_enqueue_style(\'price-package\', plugins_url(\'price-package/css/admin.css\'));
    }

    function register_widget_styles() {
        wp_enqueue_style(\'price-package\', plugins_url(\'price-package/css/widget.css\'));
    }
}

add_action(\'widgets_init\', create_function(\'\', \'register_widget("price_package");\'));
如果我犯了什么错误,请告诉我。

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

问题在于更新功能$old\\u instance是一种特殊变量。最简单的是

function update($new_instance, $old_instance) {
    return $new_instance;
}
为了保险箱

function update($new_instance, $old_instance) {
        $instance = array();
        $instance[\'name\'] = htmlentities($new_instance[\'name\']);
        $instance[\'divclass\'] = htmlentities($new_instance[\'divclass\']);
        $instance[\'paypal\'] = htmlentities($new_instance[\'paypal\']);
        return $instance;
    }

结束

相关推荐

Theme styling for plugins

我有一个插件,它有自己的CSS,用于在使用相关短代码时生成的内容。我正在尝试创建一个主题来重新设置我网站前端的样式,但由于这个插件有自己的CSS,我如何在新主题中修改它?