Skip to content

Function

Render a checkbox group setting field.

tml_admin_setting_callback_checkbox_group_field( array $args )

Parameters

$args array
Optional. An array of arguments for rendering a checkbox group.
$legend string
The legend text.
$options array
An array of options for the checkbox group.
$description string
A short description of the checkbox group.

Source

function tml_admin_setting_callback_checkbox_group_field( $args ) {
    $args = wp_parse_args(
        $args,
        array(
            'legend'      => '',
            'options'     => array(),
            'description' => '',
        )
    );

    $options = array();
    foreach ( (array) $args['options'] as $option_name => $option ) {
        $options[] = sprintf(
            '<label><input type="checkbox" name="%1$s" value="%2$s"%3$s> %4$s</label>',
            esc_attr( $option_name ),
            esc_attr( $option['value'] ),
            checked( ! empty( $option['checked'] ), true, false ),
            esc_html( $option['label'] )
        );
    }
    ?>

    <fieldset>
        <legend class="screen-reader-text"><span><?php echo esc_html( $args['legend'] ); ?></span></legend>
        <?php echo implode( "<br />\n", $options ); /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each $options entry is already built via esc_attr()/esc_html() above. */ ?>
    </fieldset>

    <?php if ( ! empty( $args['description'] ) ) : ?>
        <p class="description"><?php echo wp_kses_post( $args['description'] ); ?></p>
    <?php endif; ?>

    <?php
}

Uses

Changelog

VersionDescription
7.0 Introduced.