Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
    }
?>
share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

I believe the underlying problem is that the option array keys don't exist yet. Let's start here, in your initialization function:

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

First, this:

false == get_option( 'thanathos_theme_display_options' )

...should be this:

false === get_option( 'thanathos_theme_display_options' )

...because you're expecting an array to be returned.

Second, this:

add_option( 'thanathos_theme_display_options' );

...should be this:

add_option( 'thanathos_theme_display_options', $defaults );

...where $defaults is a defined array of default values. As it is currently, you're simply adding an empty row to the wp_options DB table, since you're not telling add_action() what values to add to your option.

While we're on the topic, I'll mention that there's a much better approach than adding default values to the DB. Instead of doing that, do something like this when you need to reference the Theme options:

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

This function will return any user-set options, while falling back to the Theme-defined defaults if the user hasn't set any options.

So for example, in your settings page form field:

// 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) . '/>';

Now, even if the user hasn't set a value for 'show_header', $options['show_header'] will return the Theme-defined default value, rather than throwing an error for the array key not being set.

share|improve this answer
add comment

Your error is not an error at all

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

A warning is not an error, an error would halt PHP execution, a warning does not ( though sometimes it can if its outputted before headers ).

This error is caused by accessing the show_header key of an array without checking beforehand that it actually exists

e.g.

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

Here 'khjbefoc4gt8t' is undefined, I never gave it a value, and it's never been encountered before, so PHP doesn't know what to print, so it'll from a warning.

This would be more sensible:

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

You could also provide defaults:

$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

So:

  • Don't access variables that might not exist without checking for them
  • Don't print the error log to the screen, that's what error logs and text editors are for
  • Provide sane defaults rather than none at all
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.