Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Wordpress Settings API error

Hi I am trying to creating some custom options for a template I am developing but I seem to be getting an error:

Warning: Illegal string offset 'show_header' in C:\xampp\htdocs\wordpress\wp-content\themes\01MyWork\includes\theme-options.php on line 62

This is the line that seems to be throwing the error:

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

And this is the entire code:

   <?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;
    }
?>

Upvotes: 0

Views: 619

Answers (3)

Edward Munch
Edward Munch

Reputation: 145

Yes, that's the problematic part:

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

That's the initial if statement at the beginning of the thanatos_initializa_theme_options() function.

You can find the solution in the very neat Theme Options API tut at http://wp.tutsplus.com/tutorials/theme-development/the-complete-guide-to-the-wordpress-settings-api-part-4-on-theme-options/#post-684925289 Or, more exactly more exactly in this article's comments section.

I'm pasting the Steve Bondy's smart solution here because for some reason making the page scroll to the appropriate comment doesn't work for me (in Chrome at least).

START quote

(...)One little issue I had - I followed along through reordering the code, up to just before adding the Social options. At that point I discovered that the code was broken. I would get an error like

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

It turns out that this error was caused by adding 'sandbox_theme_display_options' to the options table without giving it a value. If you change sandbox_initialize_theme_options as follows it will create and initialize the options, avoiding the error experienced by myself and others.

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
(...)

If the old code has been run the empty 'sandbox_theme_display_options' value must be deleted from the database first. Alternatively, the following code will also detect this case and correct it.

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
(...)

This checks for non-existant or empty options values and initializes the options using update_option instead of add_option.

EOF quote

Upvotes: 1

Matt Jensen
Matt Jensen

Reputation: 1564

You may have to run: delete_option('thanathos_theme_display_options'); during 'admin_init' if you think you have old plugin information mucking about in the database and need a fresh start.

Upvotes: 0

inigomedina
inigomedina

Reputation: 1831

You are using name="thanathos_theme_display_options[show_header]" in plain HTML. Probably you want to parse by PHP the string [show_header].

Upvotes: 0

Related Questions