问题描述

有关 wp_options 表中保存的窗口小工具实例有趣的问题。我花了很多时间,直到我想出来,但仍然不知道为什么会发生。问题:所以问题是当我拖动任何小工具并放在管理控制面板的任何侧边栏时,它的标识以 2 开头。例如,当我放置 「文本」 小工具时,它的 ID 为 widget_text-2,尽管事实上它是第一个小工具。更有趣的是,当我去 wp_options 表并且从 widget_text 选项中排序序列化数据时,我得到的是从 2 开始的索引数组。为什么?

最佳解决方案

为什么零件可以在机票 #24889 中找到。

让我 quote @azaozz:

When support for multi-use widgets was introduced, a lot of widgets got converted from “single” to “multi” at the same time. To keep backward compatibility and not break the existing widgets, there was some conversion code that would set the “single” widgets instances to *-1 (not sure why not *-0, it was a long time ago). In order to not overwrite the converted data, the new multi-widgets instances had to start form 2. As @tyxla mentions, this doesn’t break anything and if changed to -0 or -1, in theory can still overwrite someone’s single widgets.

可用的文本小工具在全局 $wp_registered_widgets 中列为:

[text-1] => Array
    (
        [name] => Text
        [id] => text-1
        [callback] => Array
            (
                [0] => WP_Widget_Text Object
                    (
                        [id_base] => text
                        [name] => Text
                        [option_name] => widget_text
                        [alt_option_name] =>
                        [widget_options] => Array
                            (
                                [classname] => widget_text
                                [customize_selective_refresh] => 1
                                [description] => Arbitrary text or HTML.
                            )

                        [control_options] => Array
                            (
                                [id_base] => text
                                [width] => 400
                                [height] => 350
                            )

                        [number] => 1
                        [id] => text-1
                        [updated] =>
                    )

                [1] => display_callback
            )

        [params] => Array
            (
                [0] => Array
                    (
                        [number] => -1
                    )

            )

        [classname] => widget_text
        [customize_selective_refresh] => 1
        [description] => Arbitrary text or HTML.
    )

我认为 1,在 text-1 id 中,来自 WP_Widget::_register()的这一部分:

if ( $empty ) {
    // If there are none, we register the widget's existence with a generic template.
    $this->_set( 1 );
    $this->_register_one();
}

当使用 wp_list_widgets(), 列出可用的多小工具时,next_widget_id_number()功能用于计算下一个小工具 ID 号。它定义为:

function next_widget_id_number( $id_base ) {
    global $wp_registered_widgets;
    $number = 1;

    foreach ( $wp_registered_widgets as $widget_id => $widget ) {
        if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) )
            $number = max($number, $matches[1]);
    }
    $number++;

    return $number;
}

对于 text-1 ID,下一个窗口小工具 ID 号为 max( 1, 1 ) + 1 = 2

所以当我们通过一个可用的侧边栏拖动第一个多文本小工具时,widget_text 选项存储为 (调整为更好的可读性):

a:2:{
i:2;a:3:{s:5:"title";s:0:"";s:4:"text";s:0:"";s:6:"filter";b:0;}s:12:"_multiwidget";i:1;
}

我们注意到 i:2 部分,sidebars_widgets 选项将包含 text_2 实例。

希望有帮助

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。