woocommerce_form_field

Outputs a checkout/address form field.

Description

woocommerce_form_field( (string) $key, (mixed) $args, (null) $value = null ); 

Parameters (3)

0. $key (string)
The key.
1. $args (mixed)
The args.
2. $value — Optional. (null) => null
(default: null)

Usage

  1. if ( !function_exists( 'woocommerce_form_field' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-template-functions.php'; 
  3.  
  4. // The key. 
  5. $key = ''; 
  6.  
  7. // The args. 
  8. $args = null; 
  9.  
  10. // (default: null) 
  11. $value = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = woocommerce_form_field($key, $args, $value); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-template-functions.php  
  1. function woocommerce_form_field( $key, $args, $value = null ) { 
  2. $defaults = array( 
  3. 'type' => 'text',  
  4. 'label' => '',  
  5. 'description' => '',  
  6. 'placeholder' => '',  
  7. 'maxlength' => false,  
  8. 'required' => false,  
  9. 'autocomplete' => false,  
  10. 'id' => $key,  
  11. 'class' => array(),  
  12. 'label_class' => array(),  
  13. 'input_class' => array(),  
  14. 'return' => false,  
  15. 'options' => array(),  
  16. 'custom_attributes' => array(),  
  17. 'validate' => array(),  
  18. 'default' => '',  
  19. 'autofocus' => '',  
  20. 'priority' => '',  
  21. ); 
  22.  
  23. $args = wp_parse_args( $args, $defaults ); 
  24. $args = apply_filters( 'woocommerce_form_field_args', $args, $key, $value ); 
  25.  
  26. if ( $args['required'] ) { 
  27. $args['class'][] = 'validate-required'; 
  28. $required = ' <abbr class="required" title="' . esc_attr__( 'required', woocommerce ) . '">*</abbr>'; 
  29. } else { 
  30. $required = ''; 
  31.  
  32. if ( is_string( $args['label_class'] ) ) { 
  33. $args['label_class'] = array( $args['label_class'] ); 
  34.  
  35. if ( is_null( $value ) ) { 
  36. $value = $args['default']; 
  37.  
  38. // Custom attribute handling 
  39. $custom_attributes = array(); 
  40. $args['custom_attributes'] = array_filter( (array) $args['custom_attributes'] ); 
  41.  
  42. if ( $args['maxlength'] ) { 
  43. $args['custom_attributes']['maxlength'] = absint( $args['maxlength'] ); 
  44.  
  45. if ( ! empty( $args['autocomplete'] ) ) { 
  46. $args['custom_attributes']['autocomplete'] = $args['autocomplete']; 
  47.  
  48. if ( true === $args['autofocus'] ) { 
  49. $args['custom_attributes']['autofocus'] = 'autofocus'; 
  50.  
  51. if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) { 
  52. foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) { 
  53. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; 
  54.  
  55. if ( ! empty( $args['validate'] ) ) { 
  56. foreach ( $args['validate'] as $validate ) { 
  57. $args['class'][] = 'validate-' . $validate; 
  58.  
  59. $field = ''; 
  60. $label_id = $args['id']; 
  61. $sort = $args['priority'] ? $args['priority'] : ''; 
  62. $field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>'; 
  63.  
  64. switch ( $args['type'] ) { 
  65. case 'country' : 
  66.  
  67. $countries = 'shipping_country' === $key ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries(); 
  68.  
  69. if ( 1 === sizeof( $countries ) ) { 
  70.  
  71. $field .= '<strong>' . current( array_values( $countries ) ) . '</strong>'; 
  72.  
  73. $field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . current( array_keys( $countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" />'; 
  74.  
  75. } else { 
  76.  
  77. $field = '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="country_to_state country_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . '>' . '<option value="">' . esc_html__( 'Select a country…', woocommerce ) . '</option>'; 
  78.  
  79. foreach ( $countries as $ckey => $cvalue ) { 
  80. $field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . $cvalue . '</option>'; 
  81.  
  82. $field .= '</select>'; 
  83.  
  84. $field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country', woocommerce ) . '" /></noscript>'; 
  85.  
  86.  
  87. break; 
  88. case 'state' : 
  89.  
  90. /** Get Country */ 
  91. $country_key = 'billing_state' === $key ? 'billing_country' : 'shipping_country'; 
  92. $current_cc = WC()->checkout->get_value( $country_key ); 
  93. $states = WC()->countries->get_states( $current_cc ); 
  94.  
  95. if ( is_array( $states ) && empty( $states ) ) { 
  96.  
  97. $field_container = '<p class="form-row %1$s" id="%2$s" style="display: none">%3$s</p>'; 
  98.  
  99. $field .= '<input type="hidden" class="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="" ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '" />'; 
  100.  
  101. } elseif ( ! is_null( $current_cc ) && is_array( $states ) ) { 
  102.  
  103. $field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="state_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ) . '"> 
  104. <option value="">' . esc_html__( 'Select a state…', woocommerce ) . '</option>'; 
  105.  
  106. foreach ( $states as $ckey => $cvalue ) { 
  107. $field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . $cvalue . '</option>'; 
  108.  
  109. $field .= '</select>'; 
  110.  
  111. } else { 
  112.  
  113. $field .= '<input type="text" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" ' . implode( ' ', $custom_attributes ) . ' />'; 
  114.  
  115.  
  116. break; 
  117. case 'textarea' : 
  118.  
  119. $field .= '<textarea name="' . esc_attr( $key ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" ' . ( empty( $args['custom_attributes']['rows'] ) ? ' rows="2"' : '' ) . ( empty( $args['custom_attributes']['cols'] ) ? ' cols="5"' : '' ) . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $value ) . '</textarea>'; 
  120.  
  121. break; 
  122. case 'checkbox' : 
  123.  
  124. $field = '<label class="checkbox ' . implode( ' ', $args['label_class'] ) . '" ' . implode( ' ', $custom_attributes ) . '> 
  125. <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 ) . ' /> ' 
  126. . $args['label'] . $required . '</label>'; 
  127.  
  128. break; 
  129. case 'password' : 
  130. case 'text' : 
  131. case 'email' : 
  132. case 'tel' : 
  133. case 'number' : 
  134.  
  135. $field .= '<input type="' . esc_attr( $args['type'] ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />'; 
  136.  
  137. break; 
  138. case 'select' : 
  139.  
  140. $options = $field = ''; 
  141.  
  142. if ( ! empty( $args['options'] ) ) { 
  143. foreach ( $args['options'] as $option_key => $option_text ) { 
  144. if ( '' === $option_key ) { 
  145. // If we have a blank option, select2 needs a placeholder 
  146. if ( empty( $args['placeholder'] ) ) { 
  147. $args['placeholder'] = $option_text ? $option_text : __( 'Choose an option', woocommerce ); 
  148. $custom_attributes[] = 'data-allow_clear="true"'; 
  149. $options .= '<option value="' . esc_attr( $option_key ) . '" ' . selected( $value, $option_key, false ) . '>' . esc_attr( $option_text ) . '</option>'; 
  150.  
  151. $field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ) . '"> 
  152. ' . $options . ' 
  153. </select>'; 
  154.  
  155. break; 
  156. case 'radio' : 
  157.  
  158. $label_id = current( array_keys( $args['options'] ) ); 
  159.  
  160. if ( ! empty( $args['options'] ) ) { 
  161. foreach ( $args['options'] as $option_key => $option_text ) { 
  162. $field .= '<input type="radio" class="input-radio ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '"' . checked( $value, $option_key, false ) . ' />'; 
  163. $field .= '<label for="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" class="radio ' . implode( ' ', $args['label_class'] ) . '">' . $option_text . '</label>'; 
  164.  
  165. break; 
  166.  
  167. if ( ! empty( $field ) ) { 
  168. $field_html = ''; 
  169.  
  170. if ( $args['label'] && 'checkbox' != $args['type'] ) { 
  171. $field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . $args['label'] . $required . '</label>'; 
  172.  
  173. $field_html .= $field; 
  174.  
  175. if ( $args['description'] ) { 
  176. $field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>'; 
  177.  
  178. $container_class = esc_attr( implode( ' ', $args['class'] ) ); 
  179. $container_id = esc_attr( $args['id'] ) . '_field'; 
  180. $field = sprintf( $field_container, $container_class, $container_id, $field_html ); 
  181.  
  182. $field = apply_filters( 'woocommerce_form_field_' . $args['type'], $field, $key, $args, $value ); 
  183.  
  184. if ( $args['return'] ) { 
  185. return $field; 
  186. } else { 
  187. echo $field;