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

2 s
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *