首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress设置API错误

Wordpress设置API错误
EN

Stack Overflow用户
提问于 2012-08-21 22:46:07
回答 3查看 564关注 0票数 0

您好,我正在尝试为我正在开发的模板创建一些自定义选项,但我似乎得到了一个错误:

代码语言:javascript
复制
Warning: Illegal string offset 'show_header' in C:\xampp\htdocs\wordpress\wp-content\themes\01MyWork\includes\theme-options.php on line 62

下面这一行似乎抛出了错误:

代码语言:javascript
复制
 $html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';  

下面是完整的代码:

代码语言:javascript
复制
   <?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;
    }
?>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-22 13:45:04

您在普通超文本标记语言中使用了name="thanathos_theme_display_options[show_header]"。您可能希望通过PHP解析字符串[show_header]

票数 0
EN

Stack Overflow用户

发布于 2013-10-12 07:49:02

是的,这就是问题所在:

代码语言:javascript
复制
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-on-theme-options/#post-684925289上非常整洁的主题选项API教程中找到解决方案,更确切地说,可以在本文的评论部分找到解决方案。

我之所以在这里粘贴Steve Bondy的聪明解决方案,是因为出于某种原因,让页面滚动到适当的评论对我来说不起作用(至少在Chrome中是这样)。

起始报价

(...)我有一个小问题--我一直在重新排序代码,直到添加Social选项之前。在这一点上,我发现代码被破解了。我会得到一个错误,比如

代码语言:javascript
复制
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‘添加到options表而没有给它赋值而导致的。如果您按如下方式更改sandbox_initialize_theme_options,它将创建并初始化选项,从而避免我和其他人遇到的错误。

代码语言:javascript
复制
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”值。或者,下面的代码也会检测到这种情况并纠正它。

代码语言:javascript
复制
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
(...)

这将检查不存在或为空的选项值,并使用update_option而不是add_option初始化选项。

EOF报价

票数 1
EN

Stack Overflow用户

发布于 2013-06-05 03:35:18

如果你认为你的数据库中有旧的插件信息,并且需要一个新的开始,你可能必须在'admin_init‘期间运行:delete_option('thanathos_theme_display_options');

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12057454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档