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.

I am trying to creating some custom options for a template I am developing but I am getting an error when the checkbox (Line 130) is unchecked:

Warning: Illegal string offset 'show_admin_dev' in E:\composite-cms\WordPress-Settings-Sandbox-master\lib\admin\pages\dev-page.php on line 11

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

if ( $options['show_admin_dev'] == 1 )

The entire code can be found on GitHub.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Your problem is that you haven't included a sanitization function as the third parameter to register_settings on line 111. When no sanitization provided WordPress deletes the value of the option and creates a new one based only on what is passed in $_POST. Since unchecked checkboxes are not sent by the browser at all you end up with $options['show_admin_dev'] being not set.

You should try to add sanitization which adds the value if it is not in the option

register_setting(
  'ccms_developer_options',
  'ccms_developer_options',
  'ccms_developer_sanit' 
);

function ccms_developer_sanit($newval) {
  if (!isset($newval['show_admin_dev'])) 
    $newval['show_admin_dev'] = 0;

  return $newval;
}
share|improve this answer
    
Thanks that fixed that issue, in short i was being a dumdum. –  CompositeUK Dec 27 '12 at 13:04
    
nah, I think the setting API is not intuative. Too easy to miss an important detail and it is harder to debug. –  Mark Kaplun Dec 27 '12 at 14:49
add comment

It appears that $options['show_admin_dev'] is actually a string, not an integer. You're trying to compare a string to an integer, which really annoys newer versions of PHP.

When setting the default option (line 69), you have show_admin_dev set to '0'. Removing the quotes around this should prevent the error.

You can also convert $options['show_admin_dev'] to an integer in the comparison using the intval() function:

if ( intval( $options['show_admin_dev'] ) == 1 )
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.