問題描述

嗨,我正在嘗試為我正在開發的模板建立一些自定義選項,但是我似乎收到一個錯誤:

Warning: Illegal string offset 'show_header' in C:xampphtdocswordpresswp-contentthemes1MyWorkincludestheme-options.php on line 62

這是似乎丟擲錯誤的行:

 $html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';

這是整個程式碼:

    <?php
    function thanatos_theme_menu(){
        add_theme_page(
                       "Thanathos Theme Options",
                       "Thanathos Theme",
                       "administrator",
                       "thanathos_theme_options",
                       "thanathos_theme_display_callback"
                      );
    }
    add_action('admin_menu' , 'thanatos_theme_menu');
    function thanathos_theme_display_callback(){
?>
         <div class="wrap">
                <div id="icon-themes" class="icon32"></div>
                <h2>Sandbox Theme Options</h2>

                <?php settings_errors(); ?>
                <!--Create the form that will be used to render our options-->
                <form method="post" action="options.php">
                    <?php settings_fields('thanathos_theme_display_options'); ?>
                    <?php do_settings_sections( 'thanathos_theme_display_options' ); ?>
                    <?php submit_button(); ?>
                </form>
        </div>
<?php
    }

    add_action('admin_init' , 'thanatos_initializa_theme_options');
    function thanatos_initializa_theme_options(){
        if( false == get_option( 'thanathos_theme_display_options' ) ) {
            add_option( 'thanathos_theme_display_options' );
        }
        add_settings_section(
                'general_settings_section',
                'Thanatos Options',
                'thanatos_general_options_callback',
                'thanathos_theme_display_options'
        );
        add_settings_field(
                'show_header',
                'Header',
                'thanathos_field_header_callback',
                'thanathos_theme_display_options',
                'general_settings_section',
                 array(                              // The array of arguments to pass to the callback. In this case, just a description.
                    'Activate this setting to display the header.'
                 )
        );
        register_setting('thanathos_theme_display_options', 'thanathos_theme_display_options');
    }

    function thanatos_general_options_callback(){
        echo 'mergem la mare';
    }
    function thanathos_field_header_callback($args){
         // First, we read the options collection
        $options = get_option('thanathos_theme_display_options');
        // Next, we update the name attribute to access this element's ID in the context of the display options array
        // We also access the show_header element of the options collection in the call to the checked() helper function
        $html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';
         // Here, we'll take the first argument of the array and add it to a label next to the checkbox
        $html .= '<label for="show_header"> '  . $args[0] . '</label>';
        echo $html;
    }
?>

最佳解決方案

我相信基本的問題是選項陣列鍵不存在。我們從這裡開始,在你的初始化函式中:

if( false == get_option( 'thanathos_theme_display_options' ) ) {
    add_option( 'thanathos_theme_display_options' );
}

首先,這個:

false == get_option( 'thanathos_theme_display_options' )

應該是這樣的:

false === get_option( 'thanathos_theme_display_options' )

… 因為你期待一個陣列被返回。

二,這個:

add_option( 'thanathos_theme_display_options' );

應該是這樣的:

add_option( 'thanathos_theme_display_options', $defaults );

… 其中 $defaults 是一個定義的預設值陣列。目前,您只需向 wp_options 資料庫表新增一個空行,因為您沒有告知 add_action()要新增哪些值。

在我們討論這個話題的時候,我會提到有一個比 DB 新增預設值更好的方法。而不是這樣做,當您需要參考主題選項時,請執行此操作:

function thanathos_get_options() {
    $defaults = array(); // define this somewhere; reference it here
    return array_merge( $defaults, get_option( 'thanathos_theme_display_options', array() ) );
}

此功能將返回任何 user-set 選項,如果使用者未設定任何選項,則返回到 Theme-defined 預設值。

所以例如在您的設定頁面窗體欄位中:

// Get Theme Options
$options = thanathos_get_options();
// Define form-field markup
 $html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';

現在,即使使用者沒有為'show_header'設定值,$options['show_header']將返回 Theme-defined 預設值,而不是為未設定的陣列鍵發出錯誤。

次佳解決方案

你的錯誤根本不是錯誤

Warning: Illegal string offset ‘show_header’ in C:xampphtdocswordpresswp-contentthemes1MyWorkincludestheme-options.php on line 62

一個警告不是錯誤,一個錯誤會阻止 PHP 的執行,一個警告不會 (儘管有時它可以如果它在頭之前輸出) 。

此錯誤是透過訪問陣列的 show_header 鍵而不事先檢查其實際存在的

例如

$test = array();
$test['foo'] = 'bar';
echo $test['khjbefoc4gt8t']; // something I made up by smushing the keyboard

在這裡’khjbefoc4gt8t’ 是未定義的,我從來沒有給它一個值,它從來沒有遇到過,所以 PHP 不知道列印什麼,所以它會從一個警告。

這更明智:

if(isset($test['khjbefoc4gt8t']){
    echo $test'khjbefoc4gt8t'];
}

您還可以提供預設值:

$test = array();

// setup defaults
$test = array_merge( array( 'khjbefoc4gt8t' => 'hello world'), $test );

// do some work
$test['foo'] = 'bar';
echo $test['khjbefoc4gt8t']; // something I made up by smushing the keyboard

所以:

  • 不檢查可能不存在的變數

  • 不要將錯誤日誌列印到螢幕上,這就是錯誤日誌和文字編輯器

  • 提供合理的預設值,而不是沒有

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。