bp_get_member_profile_data( array|string $args = '' )

Get a piece of user profile data.

Description

When used in a bp_has_members() loop, this function will attempt to fetch profile data cached in the template global. It is also safe to use outside of the loop.

Parameters

$args

(Optional) Array of config parameters.

  • 'field'
    (string) Name of the profile field.
  • 'user_id'
    (int) ID of the user whose data is being fetched. Defaults to the current member in the loop, or if not present, to the currently displayed user.

Default value: ''

Return

(string|bool) Profile data if found, otherwise false.

Source

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

1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
function bp_get_member_profile_data( $args = '' ) {
    global $members_template;
 
    if ( ! bp_is_active( 'xprofile' ) ) {
        return false;
    }
 
    // Declare local variables.
    $data = false;
 
    // Guess at default $user_id.
    $default_user_id = 0;
    if ( ! empty( $members_template->member->id ) ) {
        $default_user_id = $members_template->member->id;
    } elseif ( bp_displayed_user_id() ) {
        $default_user_id = bp_displayed_user_id();
    }
 
    $defaults = array(
        'field'   => false,
        'user_id' => $default_user_id,
    );
 
    $r = wp_parse_args( $args, $defaults );
 
    // If we're in a members loop, get the data from the global.
    if ( ! empty( $members_template->member->profile_data ) ) {
        $profile_data = $members_template->member->profile_data;
    }
 
    // Otherwise query for the data.
    if ( empty( $profile_data ) && method_exists( 'BP_XProfile_ProfileData', 'get_all_for_user' ) ) {
        $profile_data = BP_XProfile_ProfileData::get_all_for_user( $r['user_id'] );
    }
 
    // If we're in the members loop, but the profile data has not
    // been loaded into the global, cache it there for later use.
    if ( ! empty( $members_template->member ) && empty( $members_template->member->profile_data ) ) {
        $members_template->member->profile_data = $profile_data;
    }
 
    // Get the data for the specific field requested.
    if ( ! empty( $profile_data ) && ! empty( $profile_data[ $r['field'] ]['field_type'] ) && ! empty( $profile_data[ $r['field'] ]['field_data'] ) ) {
        $data = xprofile_format_profile_field( $profile_data[ $r['field'] ]['field_type'], $profile_data[ $r['field'] ]['field_data'] );
    }
 
    /**
     * Filters resulting piece of member profile data.
     *
     * @since BuddyPress 1.2.0
     * @since BuddyPress 2.6.0 Added the `$r` parameter.
     *
     * @param string|bool $data Profile data if found, otherwise false.
     * @param array       $r    Array of parsed arguments.
     */
    $data = apply_filters( 'bp_get_member_profile_data', $data, $r );
 
    /**
     * Filters the resulting piece of member profile data by field type.
     *
     * This is a dynamic filter based on field type of the current field requested.
     *
     * @since BuddyPress 2.7.0
     *
     * @param string|bool $data Profile data if found, otherwise false.
     * @param array       $r    Array of parsed arguments.
     */
    if ( ! empty( $profile_data[ $r['field'] ]['field_type'] ) ) {
        $data = apply_filters( 'bp_get_member_profile_data_' . $profile_data[ $r['field'] ]['field_type'], $data, $r );
    }
 
    return $data;
}

Changelog

Changelog
Version Description
BuddyPress 1.2.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.