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
  • Sells items exclusively on Envato Market
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?

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by