From b226b38c0c865a7ef355e120a0485b5180e91f69 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 19 Nov 2012 15:05:23 +0000 Subject: [PATCH] custom_attributes option added to woocommerce_form_field args. Pass name/value pairs. Closes #1764. --- readme.txt | 1 + woocommerce-template.php | 36 ++++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/readme.txt b/readme.txt index a7dd89e62f4..d82ea28ab74 100644 --- a/readme.txt +++ b/readme.txt @@ -220,6 +220,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Tweak - Extended woocommerce_update_options for flexibility. * Tweak - Added disabled to settings API. * Tweak - Flat rate shipping - if no rules match, and no default is set, don't return a rate. +* Tweak - custom_attributes option added to woocommerce_form_field args. Pass name/value pairs. * Fix - Added more error messages for coupons. * Fix - Variation sku updating after selection. diff --git a/woocommerce-template.php b/woocommerce-template.php index a3df105b82f..7f556f51fac 100644 --- a/woocommerce-template.php +++ b/woocommerce-template.php @@ -1242,16 +1242,24 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { 'class' => array(), 'label_class' => array(), 'return' => false, - 'options' => array() + 'options' => array(), + 'custom_attributes' => array() ); $args = wp_parse_args( $args, $defaults ); - if ( ( isset( $args['clear'] ) && $args['clear'] ) ) $after = '
'; else $after = ''; + if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = ''; $required = ( $args['required'] ) ? ' *' : ''; $args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : ''; - + + // Custom attribute handling + $custom_attributes = array(); + + if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) + foreach ( $args['custom_attributes'] as $attribute => $value ) + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"'; + switch ( $args['type'] ) { case "country" : @@ -1262,7 +1270,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field .= '' . current( array_values( $woocommerce->countries->get_allowed_countries() ) ) . ''; - $field .= ''; + $field .= ''; $field .= '

' . $after; @@ -1270,7 +1278,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

- '; foreach ( $woocommerce->countries->get_allowed_countries() as $ckey => $cvalue ) @@ -1306,14 +1314,14 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

' . $after; } elseif ( is_array( $states ) ) { $field = '

'; $field .= ''; - $field .= ' '; foreach ( $states as $ckey => $cvalue ) @@ -1326,7 +1334,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

'; $field .= ''; - $field .= ''; + $field .= ''; $field .= '

' . $after; } @@ -1336,7 +1344,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

- +

' . $after; break; @@ -1344,7 +1352,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

- +

' . $after; break; @@ -1352,7 +1360,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

- +

' . $after; break; @@ -1360,7 +1368,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

- +

' . $after; break; @@ -1374,7 +1382,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field = '

- ' . $options . '

' . $after; @@ -1382,7 +1390,7 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { break; default : - $field = apply_filters( 'woocommerce_form_field_' . $args['type'], '', $key, $args, $value ); + $field = apply_filters( 'woocommerce_form_field_' . $args['type'], '', $key, $args, $value ); break; }