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

Get the attributes for a form field.


Description Description

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


Top ↑

Parameters Parameters

$name

(string) (Optional) The field name to get attributes for.

Default value: ''

$attributes

(array) (Optional) Array of existing attributes to add.

Default value: array()


Top ↑

Return Return

(string)


Source Source

File: bp-core/bp-core-template.php

	function bp_get_form_field_attributes( $name = '', $attributes = array() ) {
		$retval = '';

		if ( empty( $attributes ) ) {
			$attributes = array();
		}

		$name = strtolower( $name );

		switch ( $name ) {
			case 'username' :
			case 'blogname' :
				$attributes['autocomplete']   = 'off';
				$attributes['autocapitalize'] = 'none';
				break;

			case 'email' :
				if ( wp_is_mobile() ) {
					$attributes['autocapitalize'] = 'none';
				}
				break;

			case 'password' :
				$attributes['spellcheck']   = 'false';
				$attributes['autocomplete'] = 'off';

				if ( wp_is_mobile() ) {
					$attributes['autocorrect']    = 'false';
					$attributes['autocapitalize'] = 'none';
				}
				break;
		}

		/**
		 * Filter the attributes for a field before rendering output.
		 *
		 * @since 2.2.0
		 *
		 * @param array  $attributes The field attributes.
		 * @param string $name       The field name.
		 */
		$attributes = (array) apply_filters( 'bp_get_form_field_attributes', $attributes, $name );

		foreach( $attributes as $attr => $value ) {
			// Numeric keyed array.
			if (is_numeric( $attr ) ) {
				$retval .= sprintf( ' %s', esc_attr( $value ) );

			// Associative keyed array.
			} else {
				$retval .= sprintf( ' %s="%s"', sanitize_key( $attr ), esc_attr( $value ) );
			}
		}

		return $retval;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
2.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.