woocommerce_wp_checkbox()
Output a checkbox input box.
No Hooks.
Returns
null. Nothing (null).
Usage
woocommerce_wp_checkbox( $field, ?WC_Data $data );
- $field(array) (required)
- Field data.
- ?WC_Data $data
- .
Default: null
woocommerce_wp_checkbox() woocommerce wp checkbox code WC 10.3.6
function woocommerce_wp_checkbox( $field, ?WC_Data $data = null ) {
global $post;
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'checkbox';
$field['style'] = isset( $field['style'] ) ? $field['style'] : '';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['value'] = $field['value'] ?? OrderUtil::get_post_or_object_meta( $post, $data, $field['id'], true );
$field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'yes';
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
/**
* These values are what get passed vis $_POST depending on if the field is checked or not. If no unchecked_value is
* provided, the $_POST will not be set. This maintains backwards compatibility where consumers would use `isset`.
*/
$field['checked_value'] = isset( $field['checked_value'] ) ? $field['checked_value'] : $field['cbvalue'];
$field['unchecked_value'] = isset( $field['unchecked_value'] ) ? $field['unchecked_value'] : null;
// Custom attribute handling.
$custom_attributes = array();
if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
foreach ( $field['custom_attributes'] as $attribute => $value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}
if ( ! empty( $field['style'] ) ) {
$custom_attributes[] = 'style="' . esc_attr( $field['style'] ) . '"';
}
if ( ! empty( $field['class'] ) ) {
$custom_attributes[] = 'class="' . esc_attr( $field['class'] ) . '"';
}
echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
echo wc_help_tip( $field['description'] );
}
// Output a hidden field so a value is POSTed if the box is not checked.
if ( ! is_null( $field['unchecked_value'] ) ) {
printf( '<input type="hidden" name="%1$s" value="%2$s" />', esc_attr( $field['name'] ), esc_attr( $field['unchecked_value'] ) );
}
printf(
'<input type="checkbox" name="%1$s" id="%2$s" value="%3$s" %4$s %5$s />',
esc_attr( $field['name'] ),
esc_attr( $field['id'] ),
esc_attr( $field['checked_value'] ),
checked( $field['value'], $field['checked_value'], false ),
implode( ' ', $custom_attributes ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
}
echo '</p>';
}