Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/09/2015 06:34:41 PM (5 years ago)
Author:
r-a-y
Message:

Core: Introduce bp_form_field_attributes() function.

bp_form_field_attributes() is a helper function to output attributes for
a given field.

The function:

  • Sets some default attributes for the username, email and password input fields to better support touchscreen devices.
  • Allows plugin developers to arbitrarily filter the attributes for the given field.
  • Is used in BP_XProfile_Type::get_edit_field_html_elements() to better filter fields by input name.

Props hnla, standardspace, boonebgorges, johnjamesjacoby.

Fixes #5914.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-template.php

    r9127 r9329  
    491491    do_action( 'bp_custom_profile_sidebar_boxes' );
    492492}
     493
     494/**
     495 * Output the attributes for a form field.
     496 *
     497 * @since BuddyPress (2.2.0)
     498 *
     499 * @param string $name       The field name to output attributes for.
     500 * @param array  $attributes Array of existing attributes to add.
     501 */
     502function bp_form_field_attributes( $name = '', $attributes = array() ) {
     503    echo bp_get_form_field_attributes( $name, $attributes );
     504}
     505    /**
     506     * Get the attributes for a form field.
     507     *
     508     * Primarily to add better support for touchscreen devices, but plugin devs
     509     * can use the 'bp_get_form_field_extra_attributes' filter for further
     510     * manipulation.
     511     *
     512     * @since BuddyPress (2.2.0)
     513     *
     514     * @param string $name       The field name to get attributes for.
     515     * @param array  $attributes Array of existing attributes to add.
     516     * @return string
     517     */
     518    function bp_get_form_field_attributes( $name = '', $attributes = array() ) {
     519        $retval = '';
     520
     521        if ( empty( $attributes ) ) {
     522            $attributes = array();
     523        }
     524
     525        $name = strtolower( $name );
     526
     527        switch ( $name ) {
     528            case 'username' :
     529            case 'blogname' :
     530                $attributes['autocomplete']   = 'off';
     531                $attributes['autocapitalize'] = 'none';
     532                break;
     533
     534            case 'email' :
     535                if ( wp_is_mobile() ) {
     536                    $attributes['autocapitalize'] = 'none';
     537                }
     538                break;
     539
     540            case 'password' :
     541                $attributes['spellcheck']   = 'false';
     542                $attributes['autocomplete'] = 'off';
     543
     544                if ( wp_is_mobile() ) {
     545                    $attributes['autocorrect']    = 'false';
     546                    $attributes['autocapitalize'] = 'none';
     547                }
     548                break;
     549        }
     550
     551        /**
     552         * Filter the attributes for a field before rendering output.
     553         *
     554         * @since BuddyPress (2.2.0)
     555         *
     556         * @param array  $attributes The field attributes
     557         * @param string $name       The field name
     558         */
     559        $attributes = (array) apply_filters( 'bp_get_form_field_attributes', $attributes, $name );
     560
     561        $attributes = array_unique( $attributes );
     562
     563        foreach( $attributes as $attr => $value ) {
     564            $retval .= sprintf( ' %s="%s"', sanitize_key( $attr ), esc_attr( $value ) );
     565        }
     566
     567        return $retval;
     568    }
    493569
    494570/**
Note: See TracChangeset for help on using the changeset viewer.