mercredi 2 août 2017

count selected checkboxes php wp widget

I'm looking for a much simpeler way (other than using the if-statement) to grab the total number of checked checkboxes inside a wordpress widget.

My code is as follows (simplified):

public function widget( $args, $instance ) {

    $no = count( $_GET['widget-columns'] ) ;

    echo '<div class="uk-child-width-1-'.esc_attr( $no ).'">';
    echo '...';
    echo '</div>';


}

And here are the checkboxes

public function form( $instance ) { ?>

    <p>
        <input id="..." name="column-value-1 widget-columns[]" type="checkbox" value="1" <?php checked( '1', $variable_column_one ); ?>/>
        <label for="...">Show Column 1</label> 
    </p>
    <p>
        <input id="..." name="column-value-2 widget-columns[]" type="checkbox" value="1" <?php checked( '1', $variable_column_two ); ?>/>
        <label for="...">Show Column 2</label> 
    </p>
    <p>
        <input id="..." name="column-value-3 widget-columns[]" type="checkbox" value="1" <?php checked( '1', $variable_column_three ); ?>/>
        <label for="...">Show Column 3</label> 
    </p>

<?php }

This causes undefined index widget-columns (for obvious reasons).

Any help in putting me on the right track on getting the count of checked checkboxes, is much appreciated.




Aucun commentaire:

Enregistrer un commentaire