bp_get_form_field_attributes

Get the attributes for a form field.

Description

(string) bp_get_form_field_attributes( (string) $name = '', (array) $attributes = array() ); 

Primarily to add better support for touchscreen devices, but plugin devs can use the bp_get_form_field_extra_attributes filter for further manipulation.

Returns (string)

Parameters (2)

0. $name — Optional. (string) => ''
The field name to get attributes for.
1. $attributes — Optional. (array) => array()
Array of existing attributes to add.

Usage

  1. if ( !function_exists( 'bp_get_form_field_attributes' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-template.php'; 
  3.  
  4. // The field name to get attributes for. 
  5. $name = ''; 
  6.  
  7. // Array of existing attributes to add. 
  8. $attributes = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_get_form_field_attributes($name, $attributes); 
  12.  

Defined (1)

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

/bp-core/bp-core-template.php  
  1. function bp_get_form_field_attributes( $name = '', $attributes = array() ) { 
  2. $retval = ''; 
  3.  
  4. if ( empty( $attributes ) ) { 
  5. $attributes = array(); 
  6.  
  7. $name = strtolower( $name ); 
  8.  
  9. switch ( $name ) { 
  10. case 'username' : 
  11. case 'blogname' : 
  12. $attributes['autocomplete'] = 'off'; 
  13. $attributes['autocapitalize'] = 'none'; 
  14. break; 
  15.  
  16. case 'email' : 
  17. if ( wp_is_mobile() ) { 
  18. $attributes['autocapitalize'] = 'none'; 
  19. break; 
  20.  
  21. case 'password' : 
  22. $attributes['spellcheck'] = 'false'; 
  23. $attributes['autocomplete'] = 'off'; 
  24.  
  25. if ( wp_is_mobile() ) { 
  26. $attributes['autocorrect'] = 'false'; 
  27. $attributes['autocapitalize'] = 'none'; 
  28. break; 
  29.  
  30. /** 
  31. * Filter the attributes for a field before rendering output. 
  32. * @since 2.2.0 
  33. * @param array $attributes The field attributes. 
  34. * @param string $name The field name. 
  35. */ 
  36. $attributes = (array) apply_filters( 'bp_get_form_field_attributes', $attributes, $name ); 
  37.  
  38. foreach( $attributes as $attr => $value ) { 
  39. $retval .= sprintf( ' %s="%s"', sanitize_key( $attr ), esc_attr( $value ) ); 
  40.  
  41. return $retval;