438 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $5,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Located in Europe
+1 more
Pixelous says
Hi, I have such options for checkboxes:
function social_displays() {
    $social_display_options = array(
        'facebook_display' => array(
            'value' => 0,
            'label' => __( 'Facebook', 'mythemename' ),
        ),
        'twitter_display' => array(
            'value' => 0,
            'label' => __( 'Twitter', 'mythemename' ),
        ),
        ),
    );

    return apply_filters( 'social', $social_display_options );
}
And have default options for checkboxes:
function get_default_social_options() {
    $default_social_display_options = array(
        'facebook_display' => 0,
        'twitter_display' => 0,
    );

    return apply_filters( 'default_social_options', $default_social_display_options );
}
Then I have the render field:
function settings_field_social_display() {
    $options = get_social_options();
    foreach ( social_displays() as $k => $social_display ) {
        ?>
        <label class="description">
        <input name="social_options[<?php echo $k; ?>]" type="checkbox" id="<?php echo $k; ?>" value="1" <?php checked( $options[$k], 1 ); ?> />
        <?php echo $social_display['label']; ?><br />
        <?php
    }

}
Then this function:
function get_social_options() {
    return get_option( 'social_options', get_default_social_options() );
}
And function for saving values:
function social_options_validate( $input ) {
    $output = $defaults = get_default_social_options();

    // Social Display must be in our array of social options
    foreach ( social_displays() as $k => $social_display ) {
        if ( isset( $input[$k] ) )
            $output[$k] = $input[$k];
    }

    return apply_filters( 'social_options_validate', $output, $input, $defaults );

}

Everything is work fine when saving the values. But if I need checkboxes checked by default then I replace 0 to 1 but in this case values don’t saves. :( Could someone say what’s the matter please?

by
by
by
by
by
by