BuddyDev

Search

Apply Filter to Xprofile Value Prior to Display of User Profile

Tagged: ,

  • Participant
    Level: Initiated
    Posts: 19
    Quint A. Rahaman, Jr. on #45137

    Use Case:

    1. Added number field to capture user “height (inches)”
    2. Apply conversion function to display value in feet and inches on the user’s Profile.

    
    function inches2feet($inches){
    
        //Get the feet by dividing the inches by 12.
        $feet = intval($inches/12);
        //Then, get the remainder of that division.
        $inches = $inches % 12;
        //If there is no remainder, then it's an even figure
        if($inches == 0){
            return $feet . ' foot';
        }
        //Otherwise, return it in ft and inches.
        return sprintf('%d ft %d inches', $feet, $inches);
    }
    

    This inches2feet function works when used to output on the frontend in the loop, where “8” is the “height” field ID:

    
    <?php $inches = floatval(xprofile_get_field_data( 8, $user_id)); ?>
    <?php $pb_inches_conv = inches2feet($inches); ?>
    <p> Height: <?php echo $pb_inches_conv; ?> </p>
    

    Request 1:

    I would like to apply the same function to the “height” field so that when the User visits their Profile, “height” is shown in feet and inches.

    Request 2:

    Append the metric converted value in parentheses next to the feet and inches value.

    Any suggestions would be most welcome.

    Recommendation: Add a “height” field type to BuddyPress Xprofile Custom Field Types which allows the the user to choose between entering data in imperial or metric

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #45142

    Hi Quint,
    Welcome back.

    1. You can apply it to multiple fields in the profile loop using a filter

    
    
    add_filter( 'bp_get_the_profile_field_value', function ( $field_value, $field_type = '', $field_id = '' ) {
    	if ( ! $field_id || ! $field_value ) {
    		return $field_value;
    	}
    
    	$height_fields = array( 8, 9 );// update your field ids.
    	if ( ! in_array( $field_id, $height_fields ) ) {
    		return $field_value;
    	}
    
        return inches2feet( floatval( $field_value ) );
    
    }, 9, 3 );
    
    

    2. Do you want me to update to append the original value. If yes, what would be the display format.

    3. Thank you for the suggestion and I am surely considering adding a length field.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 19
    Quint A. Rahaman, Jr. on #45143

    Glad to be back!

    Item 1. Added the snippet. It works! Thanks!

    Item 2. Yes, I would like the metric conversion appended to the feet/inches value. I live in the States, so I really don’t know how you and ROW expresses their height in metric. Whatever makes sense to you or any user of the metric system is the format that should be used. Your call.

    Item3. Cool. I think that would be very helpful to every BuddyPress/BuddyBoss user.

    Probably in the next two or three days, I will submit a small project to you. I hope you can help.

    Cheers,

    Quint

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #45144

    Hi Quint,
    Thank you for the reply and confirming.

    2. I have slightly updated the code( Also included your code for inch2feet, so please remove the old one).

    
    
    function inches2feet($inches){
    
    	//Get the feet by dividing the inches by 12.
    	$feet = intval($inches/12);
    	//Then, get the remainder of that division.
    	$inches = $inches % 12;
    	//If there is no remainder, then it's an even figure
    	if($inches == 0){
    		return $feet . ' foot';
    	}
    	//Otherwise, return it in ft and inches.
    	return sprintf('%d ft %d inches', $feet, $inches);
    }
    
    /**
     * Converts inches to metric unit(zz meters, xx cms ).
     *
     * @param float $inches inch.
     *
     * @return string
     */
    function inches2metric( $inches ) {
    
    	$cms = $inches * 2.54;
    
    	$len_meters = floor( $cms / 100 );
    	$len_cms    = $cms % 100;
    
    	$out = array();
    
    	if ( $len_meters ) {
    		$out [] = sprintf( '%d meters', $len_meters );
    	}
    	if ( $len_cms ) {
    		$out [] = sprintf( '%d cm', $len_cms );
    	}
    
    	return join( ', ', $out );
    
    }
    
    add_filter( 'bp_get_the_profile_field_value', function ( $field_value, $field_type = '', $field_id = '' ) {
    	if ( ! $field_id || ! $field_value ) {
    		return $field_value;
    	}
    
    	$height_fields = array( 8, 9 );// update your field ids.
    	if ( ! in_array( $field_id, $height_fields ) ) {
    		return $field_value;
    	}
    
    	$field_value = floatval( $field_value );
    
    	$imperial_length = inches2feet( floatval( $field_value ) );
    	$metric_length   = inches2metric( $field_value );
    
    	return "{$imperial_length}({$metric_length})";
    
    }, 9, 3 );
    
    

    It will display in the format ” xx ft, yy inch( mm meters, zz cms)”.
    Please feel free to adapt it.

    3. Thank you. I do believe that.

    Sure, I am looking forward to it.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved