So checked is fairly simple to understand. It compares the first two values. If they’re equal, it spits out checked="checked"
if they aren’t equal nothing happens.
<?php
$saved = 'on';
$compare="on"
// spits out checked="checked"
checked($saved, $compare);
$saved = 'off';
// does nothing
checked($saved, $compare);
How you save check boxes it up to you. Because of the way things get serialized, I like to save “checked” values as on
and unchecked values as off
.
So to sanitize you’d do something like this:
<?php
function wpse74685_clean_option($dirty)
{
$clean = array(
'post_sharing_networks' => array(),
);
// $dirty['post_sharing_networks'] will contain the array of
// of your checkbox values, so check to make sure its there
// then loop through.
if(!empty($dirty['post_sharing_networks']))
{
// if the box was checked, the value will be there
// if not nothing will be there.
foreach(post_sharing_networks() as $network)
$clean['post_sharing_networks'][$network['value']] = !empty($dirty['post_sharing_networks'][$network['value']]) ? 'on' : 'off';
}
return $clean;
}
Then you calls to checked will be something like this:
<input type="checkbox" value="1"
name="theme_options[post_sharing_networks][<?php echo esc_attr($network['value']); ?>]"
<?php checked($post_sharing_networks[$network['value']], 'on'); ?> />