WordPress Settings API – Allow Multiple Checkbox Selections Save to Database

Using the WooCommerce plugin, I’m creating a checkbox for each attribute available. My issue is, when selecting multiple checkbox, only one selection is pushed into the database. My code is below…

function faceted_search_init_fn(){

// set database functions class
global $wpdb;

$attribute_values = $wpdb->get_results("SELECT slug FROM wp_terms INNER JOIN wp_term_taxonomy INNER JOIN wp_woocommerce_attribute_taxonomies WHERE SUBSTR(wp_term_taxonomy.taxonomy, 4) = wp_woocommerce_attribute_taxonomies.attribute_name AND wp_term_taxonomy.term_id = wp_terms.term_id", ARRAY_A);

register_setting('faceted_options', 'faceted_options' );
add_settings_section('main_section', 'Main Settings', 'section_text_fn', __FILE__);
add_settings_field('plugin_chk', 'My Checkbox', 'setting_chk_fn', __FILE__, 'main_section', $attribute_values);

}

Read More
function setting_chk_fn($attribute_values) {    
    foreach($attribute_values as $attribute_value) {
        echo $attribute_value['slug'];
        echo "<input id='plugin_chk' name='faceted_options[noindex]' type='checkbox' value='" . $attribute_value['slug'] . "' />";
    }
}

I appreciate any insight.

Related posts

Leave a Reply