开发者

Upgrading WordPress Widgets to API Without Breaking Old Ones

Back in the day I built a twitter widget plugin that used the now-deprecated:

register_sidebar_widget()

I want to upgrade to the widget API from WordPress 2.8, but I also don't want to break all the existing installs. Is there a way to tell my new widget to pull settings from the 开发者_如何学Goold widget if it exists?

I've been doing some testing and it looks like I am able to pull my old settings in temporarily, but I'm running into trouble duplicating the old widget instance in the sidebar. When I upload the new code (even though my objects are named the same), it yanks out the old widget from the sidebar and I have to add it in again.

Old Code:

// 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);

New Code:

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 );
    }


Following your example, I found the following code should work:

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');
}

}

... where I'm referring to the old settings with get_option('widget_reliabletwitter_control') and delete_option('widget_reliabletwitter_control') ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜