問題描述

有關 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。