Skip to content

Function

Render a radio group setting field.

tml_admin_setting_callback_radio_group_field( array $args )

Parameters

$args array
Optional. An array of arguments for rendering a radio group.
$label_for string
The radio button name/ID.
$legend string
The legend text.
$options array
An array of options for the radio group.
$checked string
The value of the checked option.
$description string
A short description of the radio group.

Source

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

    $options = array();
    foreach ( (array) $args['options'] as $value => $label ) {
        $options[] = sprintf(
            '<label><input type="radio" name="%1$s" value="%2$s"%3$s> %4$s</label>',
            esc_html( $args['label_for'] ),
            esc_attr( $value ),
            checked( $args['checked'], $value, false ),
            esc_html( $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.