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:xampphtdocswordpresswp-contentthemes1MyWorkincludestheme-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;
}
?>
I believe the underlying problem is that the option array keys don’t exist yet. Let’s start here, in your initialization function:
First, this:
…should be this:
…because you’re expecting an array to be returned.
Second, this:
…should be this:
…where
$defaults
is a defined array of default values. As it is currently, you’re simply adding an empty row to thewp_options
DB table, since you’re not tellingadd_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:
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:
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.Your error is not an error at all
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 existse.g.
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:
You could also provide defaults:
So: