Last active
September 1, 2021 16:47
-
-
Save brandonkramer/1132cf5908cc86552ae88c28916eb959 to your computer and use it in GitHub Desktop.
Move labels after input fields in WooCommerce to make pseudo class possible to create custom checkboxes in CSS. Input fields are placed in labels by default by WooCommerce. We want to make sure the label is placed after the input field so :before pseudo class for +checked labels can be attached to create custom checkboxes in CSS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_form_field_checkbox', 'changeCheckboxFormField', 10, 4 ); | |
/** | |
* Input fields are placed in labels by default by WooCommerce. | |
* We want to make sure the label is placed after the input field so :before pseudo class for +checked labels | |
* can be attached to create custom checkboxes in CSS. For this to happen within the filter, | |
* a part of the function from woocommerce_form_field needs to be copied so we can | |
* re-create the Field HTML with the necessary variables and change the order in the $field | |
* variable and pass it to $feld_html | |
* @see woocommerce_form_field | |
*/ | |
function changeCheckboxFormField ( $field, $key, $args, $value ) | |
{ | |
$custom_attributes = array(); | |
$args['custom_attributes'] = array_filter( (array) $args['custom_attributes'], 'strlen' ); | |
if ( $args['maxlength'] ) { | |
$args['custom_attributes']['maxlength'] = absint( $args['maxlength'] ); | |
} | |
if ( ! empty( $args['autocomplete'] ) ) { | |
$args['custom_attributes']['autocomplete'] = $args['autocomplete']; | |
} | |
if ( true === $args['autofocus'] ) { | |
$args['custom_attributes']['autofocus'] = 'autofocus'; | |
} | |
if ( $args['description'] ) { | |
$args['custom_attributes']['aria-describedby'] = $args['id'] . '-description'; | |
} | |
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) { | |
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) { | |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; | |
} | |
} | |
if ( $args['required'] ) { | |
$args['class'][] = 'validate-required'; | |
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>'; | |
} else { | |
$required = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>'; | |
} | |
$sort = $args['priority'] ? $args['priority'] : ''; | |
$field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>'; | |
$label_id = $args['id']; | |
$field_html = ''; | |
// BOF custom code | |
$field = '<input type="' . esc_attr( $args['type'] ) . '" class="input-checkbox ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="1" ' . checked( $value, 1, false ) . ' />' . | |
'<label class="checkbox ' . implode( ' ', $args['label_class'] ) . '" ' . implode( ' ', $custom_attributes ) . ' for="'.esc_attr( $args['id'] ).'"> ' . $args['label'] . $required . '</label>'; | |
// EOF custom code | |
if ( $args['label'] && 'checkbox' !== $args['type'] ) { | |
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . $args['label'] . $required . '</label>'; | |
} | |
$field_html .= '<span class="woocommerce-input-wrapper">' . $field; | |
if ( $args['description'] ) { | |
$field_html .= '<span class="description" id="' . esc_attr( $args['id'] ) . '-description" aria-hidden="true">' . wp_kses_post( $args['description'] ) . '</span>'; | |
} | |
$field_html .= '</span>'; | |
$container_class = esc_attr( implode( ' ', $args['class'] ) ); | |
$container_id = esc_attr( $args['id'] ) . '_field'; | |
$field = sprintf( $field_container, $container_class, $container_id, $field_html ); | |
return $field; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment