0

こんにちは私は開発中のテンプレートのカスタムオプションを作成しようとしていますが、エラーが発生しているようです:

Warning: Illegal string offset 'show_header' in C:\xampp\htdocs\wordpress\wp-content\themes\01MyWork\includes\theme-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;
    }
?>
4

3 に答える 3

1

はい、それが問題の部分です:

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

これは、thanatos_initializa_theme_options() 関数の先頭にある最初の if ステートメントです。

解決策は、 http: //wp.tutsplus.com/tutorials/theme-development/the-complete-guide-to-the-wordpress-settings-api-part-4- の非常にきちんとした Theme Options API tut で見つけることができます。 on-theme-options/#post-684925289 または、より正確には、この記事のコメント セクションを参照してください。

なんらかの理由で適切なコメントまでページをスクロールしても (少なくとも Chrome では) うまくいかないため、Steve Bondy のスマート ソリューションをここに貼り付けます。

見積もりを開始

(...)私が抱えていた小さな問題 - ソーシャル オプションを追加する直前まで、コードの順序を変更しました。その時点で、コードが壊れていることがわかりました。次のようなエラーが表示されます

Warning: Illegal string offset 'show_header' in ...\themes\Sandbox\functions.php
Warning: Illegal string offset 'show_content' in ...\themes\Sandbox\functions.php
Warning: Illegal string offset 'show_footer' in ...\themes\Sandbox\functions.php

このエラーは、値を指定せずに「sandbox_theme_display_options」をオプション テーブルに追加したことが原因であることがわかりました。次のように sandbox_initialize_theme_options を変更すると、オプションが作成および初期化され、自分や他の人が経験したエラーが回避されます。

function sandbox_initialize_theme_options() { 
     // If the theme options don't exist, create them. 
     if( false == get_option( 'sandbox_theme_display_options' ) ) { 
          $options = array("show_header" => TRUE, "show_content" => TRUE, "show_footer" => TRUE);
          add_option( 'sandbox_theme_display_options', $options);
     } // end if
(...)

古いコードが実行された場合、空の 'sandbox_theme_display_options' 値を最初にデータベースから削除する必要があります。または、次のコードもこのケースを検出して修正します。

function sandbox_initialize_theme_options() { 
     // See if the options exist, and initialize them if they don't
     $options = get_option( 'sandbox_theme_display_options' );
     if( false == $options or $options == "" ) { 
          $options = array("show_header" => TRUE, "show_content" => TRUE, "show_footer" => TRUE);
          update_option( 'sandbox_theme_display_options', $options);
     } // end if
(...)

これは、存在しないまたは空のオプション値をチェックし、add_option の代わりに update_option を使用してオプションを初期化します。

EOFの引用

于 2013-10-11T23:49:02.333 に答える
0

name="thanathos_theme_display_options[show_header]"プレーンな HTML で使用しています。おそらく、PHP で文字列を解析したいでしょう[show_header]

于 2012-08-22T05:45:04.400 に答える
0

delete_option('thanathos_theme_display_options'); 古いプラグイン情報がデータベースをいじっていて、新たなスタートが必要だと思われる場合は、次 を実行する必要があるかもしれません: during 'admin_init'。

于 2013-06-04T19:35:17.653 に答える