当天我建立了一个使用现已弃用的推特小部件插件:
register_sidebar_widget()
我想从WordPress 2.8升级到widget API,但我也不想破坏所有现有的安装。有没有办法告诉我的新小部件从旧小部件中提取设置(如果存在?)
我一直在进行一些测试,看起来我可以临时取出旧设置,但是我在边栏中复制旧的widget实例时遇到了麻烦。当我上传新代码时(即使我的对象命名相同),它会从侧边栏中抽出旧的小部件,我必须再次添加它。
旧代码:
// Run code and init
add_action('widgets_init', 'widget_reliabletwitter_init');
function widget_reliabletwitter_init() {
// Register widget for use
register_sidebar_widget(array('Reliable Twitter', 'widgets'), 'widget_reliabletwitter');
// Register settings for use, 325x400 pixel form
register_widget_control(array('Reliable Twitter', 'widgets'), 'widget_reliabletwitter_control', 325, 400);
新守则:
add_action('widgets_init', 'reliable_twitter_load_widgets');
function reliable_twitter_load_widgets() {
register_widget('Reliable_Twitter');
}
class Reliable_Twitter extends WP_Widget {
/**
* Widget setup.
*/
function Reliable_Twitter() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'widget_reliabletwitter', 'description' => __('Adds a sidebar widget to display Twitter updates and uses the more-reliable Google AJAX API.', 'widget_reliabletwitter') );
/* Widget control settings. */
$control_ops = array( 'width' => 325, 'height' => 400, 'id_base' => 'widget_reliabletwitter' );
/* Create the widget. */
$this->WP_Widget( 'widget_reliabletwitter', __('Reliable Twitter', 'widget_reliabletwitter'), $widget_ops, $control_ops );
}
答案 0 :(得分:1)
按照您的示例,我发现以下代码应该有效:
class Reliable_Twitter extends WP_Widget {
/**
* Widget setup.
*/
function Reliable_Twitter() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'widget_reliabletwitter', 'description' => __('Adds a sidebar widget to display Twitter updates and uses the more-reliable Google AJAX API.', 'widget_reliabletwitter') );
/* Widget control settings. */
$control_ops = array( 'width' => 325, 'height' => 400, 'id_base' => 'widget_reliabletwitter' );
/* Create the widget. */
$this->WP_Widget( 'widget_reliabletwitter', __('Reliable Twitter', 'widget_reliabletwitter'), $widget_ops, $control_ops );
/* Attempt upgrade from pre 2.8.4 widgets */
if ( $old_options = get_option('widget_reliabletwitter_control') ) {
$old_options['_multiwidget'] = 1;
update_option('widget_widget_reliabletwitter', $old_options);
delete_option('widget_reliabletwitter_control');
}
}
...我指的是带有get_option('widget_reliabletwitter_control')和delete_option('widget_reliabletwitter_control')的旧设置......