• I created a new setting field with multiple checkboxes in my WordPress admin. However the value of the field new_setting not saved correctly. It should be an array, but after I do var_dump(get_option( 'new_setting')) I get string(5) "Array"

    What I must change to save multiple checkboxes in a correct way?

    Here is my code:

    class clinto_general_settings {
        function clinto_general_settings() {
            add_filter( 'admin_init' , array( &$this , 'clinto_register_fields' ) );
        }
        function clinto_register_fields() {
            register_setting( 'general', 'new_setting', 'esc_attr' );
            add_settings_field('mk_new_setting', '<label for="new_setting">'.__('Active directors' , 'new_setting' ).'</label>' , array(&$this, 'clinto_fields_html') , 'general' );
        }
        function clinto_fields_html() {
            $value = get_option( 'new_setting');
    
            $terms = get_terms( 'director', array(
                'orderby'    => 'term_order',
                'hide_empty' => 0, ));
    
                if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
                     foreach ( $terms as $term ) {
                        echo '<input id="new_setting'.$term->term_id.'" type="checkbox"  name="new_setting[]" value="'.$term->term_id.'" />';
                        echo '<label for="new_setting'.$term->term_id.'">'.$term->name.'</label><br/>';
                     }
                 }
        }
    }
    
    $clinto_general_settings = new clinto_general_settings();
Viewing 1 replies (of 1 total)
  • anonymized-13749270

    (@anonymized-13749270)

    When trying to save multiple checkboxes, I see you have done the right thing first adding [] after the name to set an array of checked buttons.

    As saving to the database ( update_option() etc), you should convert the array created into a string. The best way to do it is implode commas:

    implode( ',', $myArray );

    So in your case, you saved the string Array instead.

    Here is a quick code I put together hopefully it helps:

    <?php
    
    if( isset( $_POST['my_checkbox'] ) ) {
    
    	$options = implode(',', $_POST['my_checkbox']);
    	update_option('my_checkbox', $options);
    
    }
    
    $checked_buttons = get_option('my_checkbox') !== '' ? explode(',', get_option('my_checkbox')) : array();
    
    ?>
    <form method="post">
    	<label><input type="checkbox" name="my_checkbox[]" value="1" <?php echo in_array( '1', $checked_buttons ) ? 'checked' : ''; ?>/>1</label>
    	<label><input type="checkbox" name="my_checkbox[]" value="2" <?php echo in_array( '2', $checked_buttons ) ? 'checked' : ''; ?>/>2</label>
    	<label><input type="checkbox" name="my_checkbox[]" value="3" <?php echo in_array( '3', $checked_buttons ) ? 'checked' : ''; ?>/>3</label>
    	<button>save</button>
    </form>

    After saving the comma separated checked option values, you can do the reverse now, as for knowing which options were checked.

    We produce an array from our get_option('new_setting') by exploding commas this time:

    $checked = explode(',', get_option('new_setting'));

    $checked is now an array of checked buttons saved in the option. To know if a checkbox is checked, we basically see if the value of that checkbox is within the array of $checked:

    in_array('option value', $checked)

    which is going to return bool (true or false)

    So

    echo '<input id="new_setting'.$term->term_id.'" type="checkbox" name="new_setting[]" value="'.$term->term_id.'"', in_array($term->term_id, $checked) ? ' checked="checked"' : '',' />';

    Hope that helps.

    Samuel

Viewing 1 replies (of 1 total)

The topic ‘Register setting not working with multiple checkbox’ is closed to new replies.