Yoast_Form::checkbox()publicYoast 2.0

Create a Checkbox input field.

Метод класса: Yoast_Form{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$Yoast_Form = new Yoast_Form();
$Yoast_Form->checkbox( $variable, $label, $label_left, $attr );
$variable(строка) (обязательный)
The variable within the option to create the checkbox for.
$label(строка) (обязательный)
The label to show for the variable.
$label_left(true|false)
Whether the label should be left (true) or right (false).
По умолчанию: false
$attr(массив)
Extra attributes to add to the checkbox.
По умолчанию: []

Список изменений

С версии 2.0 Введена.

Код Yoast_Form::checkbox() Yoast 24.0

public function checkbox( $variable, $label, $label_left = false, $attr = [] ) {
	$val = $this->get_field_value( $variable, false );

	$defaults = [
		'disabled' => false,
	];
	$attr     = wp_parse_args( $attr, $defaults );

	if ( $val === true ) {
		$val = 'on';
	}

	$class = '';
	if ( $label_left !== false ) {
		$this->label( $label_left, [ 'for' => $variable ] );
	}
	else {
		$class = 'double';
	}

	$disabled_attribute = $this->get_disabled_attribute( $variable, $attr );

	// phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
	echo '<input class="', esc_attr( 'checkbox ' . $class ), '" type="checkbox" id="', esc_attr( $variable ), '" name="', esc_attr( $this->option_name . '[' . $variable . ']' ), '" value="on"', checked( $val, 'on', false ), $disabled_attribute, '/>';

	if ( ! empty( $label ) ) {
		$this->label( $label, [ 'for' => $variable ] );
	}

	echo '<br class="clear" />';
}