[buddypress-trac] [BuddyPress Trac] #6132: Custom xprofile field: email

buddypress-trac noreply at wordpress.org
Sat Jan 17 08:55:10 UTC 2015


#6132: Custom xprofile field: email
-------------------------+------------------------------
 Reporter:  sooskriszta  |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Core         |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |
-------------------------+------------------------------

Comment (by sbrajesh):

 Sounds good.
 here is an implementation

 {{{
 <?php

 /**
  * Email xprofile field type.
  *
  * @since BuddyPress (2.2.0)
  */
 class BP_XProfile_Field_Type_Email extends BP_XProfile_Field_Type {

         /**
          * Constructor for the Email field type
          *
          * @since BuddyPress (2.2.0)
          */
         public function __construct() {

                 parent::__construct();

                 $this->category = _x( 'Single Fields', 'xprofile field
 type category', 'buddypress' );
                 $this->name     = _x( 'Email', 'xprofile field type',
 'buddypress' );


                 do_action( 'bp_xprofile_field_type_email', $this );
         }

         public function is_valid( $val ){

                 return is_email( $val );
         }
         /**
          * Output the edit field HTML for this field type.
          *
          * Must be used inside the {@link bp_profile_fields()} template
 loop.
          *
          * @param array $raw_properties Optional key/value array of
          *        {@link http://dev.w3.org/html5/markup/input.number.html
 permitted attributes}
          *        that you want to add.
          * @since BuddyPress (2.1.0)
          */
         public function edit_field_html( array $raw_properties = array() )
 {

                 // `user_id` is a special optional parameter that certain
 other
                 // fields types pass to {@link
 bp_the_profile_field_options()}.
                 if ( isset( $raw_properties['user_id'] ) ) {
                         unset( $raw_properties['user_id'] );
                 }
                 //bp_xprofile_extended_helper()->set_shown(
 bp_get_the_profile_field_id() );

                 $r = bp_parse_args( $raw_properties, array(
                         'type'      => 'email',
                         //'inputmode' => 'email',
                         'value'     => esc_attr(
 bp_get_the_profile_field_edit_value() ),
                 ) ); ?>

                 <label for="<?php bp_the_profile_field_input_name(); ?>">
                         <?php bp_the_profile_field_name(); ?>
                         <?php if ( bp_get_the_profile_field_is_required()
 ) : ?>
                                 <?php esc_html_e( '(required)',
 'buddypress' ); ?>
                         <?php endif; ?>
                 </label>

                 <?php do_action( bp_get_the_profile_field_errors_action()
 ); ?>

                 <input <?php echo $this->get_edit_field_html_elements( $r
 ); ?>>

                 <?php
         }

         /**
          * Output HTML for this field type on the wp-admin Profile Fields
 screen.
          *
          * Must be used inside the {@link bp_profile_fields()} template
 loop.
          *
          * @param array $raw_properties Optional key/value array of
 permitted
          *        attributes that you want to add.
          * @since BuddyPress (2.1.0)
          */
         public function admin_field_html( array $raw_properties = array()
 ) {

                 $r = bp_parse_args( $raw_properties, array(
                         'type' => 'email'
                 ) ); ?>

                 <input <?php echo $this->get_edit_field_html_elements( $r
 ); ?>>

                 <?php
         }

         /**
          * This method usually outputs HTML for this field type's children
 options
          * on the wp-admin Profile Fields "Add Field" and "Edit Field"
 screens, but
          * for this field type, we don't want it, so it's stubbed out.
          *
          * @param BP_XProfile_Field $current_field The current profile
 field on the add/edit screen.
          * @param string $control_type Optional. HTML input type used to
 render the current field's child options.
          * @since BuddyPress (2.2.0)
          */
         public function admin_new_field_html( BP_XProfile_Field
 $current_field, $control_type = '' ) {}



         /**
          * Format URL values for display.
          *
          * @since BuddyPress (2.1.0)
          *
          * @param string $field_value The URL value, as saved in the
 database.
          * @return string URL converted to a link.
          */
         public static function display_filter( $field_value ) {
                 $link      = strip_tags( $field_value );

                 //$url_text  = str_replace( $link, $no_scheme,
 $field_value );
                 return '<a href="mailto:' . esc_attr( $field_value ) . '"
 rel="nofollow">' . esc_html( $field_value ) . '</a>';
         }
 }
 }}}

 All it needs to be done is hook into the 'bp_xprofile_get_field_types' and
 add the email field using the above class.

--
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/6132#comment:1>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac


More information about the buddypress-trac mailing list