acf_get_checkbox_input()ACF 5.0.0

acf_get_checkbox_input

Returns the HTML of a checkbox input.

No Hooks.

Returns

string.

Usage

acf_get_checkbox_input( $attrs );
$attrs(array)
The array of attrs.
Default: array()

Changelog

Since 5.0.0 Introduced.

acf_get_checkbox_input() code ACF 6.8.8

function acf_get_checkbox_input( $attrs = array() ) {

	// Allow radio or checkbox type.
	$attrs = wp_parse_args(
		$attrs,
		array(
			'type' => 'checkbox',
		)
	);

	// Get label.
	$label = '';
	if ( isset( $attrs['label'] ) ) {
		$label = $attrs['label'];
		unset( $attrs['label'] );
	}

	// Render.
	$checked = isset( $attrs['checked'] );

	// Build label attributes array for accessibility and consistency.
	$label_attrs = array();
	if ( $checked ) {
		$label_attrs['class'] = 'selected';
	}

	if ( ! empty( $attrs['button_group'] ) ) {
		unset( $attrs['button_group'] );
		// If tabindex is provided, use it for the label; otherwise, use checked-based default.
		if ( isset( $attrs['tabindex'] ) ) {
			$label_attrs['tabindex'] = (string) $attrs['tabindex'];
			unset( $attrs['tabindex'] );
		} else {
			$label_attrs['tabindex'] = $checked ? '0' : '-1';
		}
		$label_attrs['role']         = 'radio';
		$label_attrs['aria-checked'] = $checked ? 'true' : 'false';
	}

	return '<label' . ( acf_esc_attrs( $label_attrs ) ? ' ' . acf_esc_attrs( $label_attrs ) : '' ) . '><input ' . acf_esc_attrs( $attrs ) . '/> ' . acf_esc_html( $label ) . '</label>';
}