BuddyPress Profile Field Display Shortcode
This documentation provides an overview of how to use a WordPress shortcode to display BuddyPress profile fields. The shortcode is highly customizable, allowing you to control how the field values are displayed, including handling repeater fields.
Function Overview
wbcom_designs_display_profile_field
This function retrieves and displays the value(s) of a specific BuddyPress profile field for a given user.
Parameters:
int $user_id— The user ID.string $field_name— The field name to retrieve.string $group_key— The group key under which the field is stored.bool $single_value— Whether to return only the first value (for repeater fields). Default isfalse.bool $value_only— Whether to return only the field value(s) without HTML wrapping. Default isfalse.
Returns:
string— The HTML or plain text output for the field.
wbcom_designs_profile_field_shortcode
This function registers a WordPress shortcode [wbcom_profile_field] that allows you to display a specific BuddyPress profile field.
Shortcode Attributes:
user_id— The user ID for which to display the profile field. Defaults to the displayed user.field_name— The name of the field you want to display (required).group_key— The group key under which the field is stored (required).single_value— Passtrueif you want to display only the first value of a repeater field. Defaults tofalse.value_only— Passtrueif you want to display only the field value(s) without any HTML structure. Defaults tofalse.
Usage Examples
1. Display a Field with HTML
[wbcom_profile_field user_id="1" field_name="bprms_location" group_key="bprm_contact_details"]
2. Display Only the Field Value (No HTML)
[wbcom_profile_field user_id="1" field_name="bprms_location" group_key="bprm_contact_details" value_only="true"]
3. Display Only the First Value of a Repeater Field
[wbcom_profile_field user_id="1" field_name="bprms_location" group_key="bprm_contact_details" single_value="true"]
4. Display Only the First Value of a Repeater Field without HTML
[wbcom_profile_field user_id="1" field_name="bprms_location" group_key="bprm_contact_details" single_value="true" value_only="true"]
Full Code Example
/**
* Display specific BuddyPress profile field value(s).
*
* @param int $user_id The user ID.
* @param string $field_name The field name to retrieve.
* @param string $group_key The group key under which the field is stored.
* @param bool $single_value Whether to return only the first value (for repeaters).
* @param bool $value_only Whether to return only the field value(s) without HTML wrapping.
* @return string The HTML or plain text output for the field.
*/
function wbcom_designs_display_profile_field( $user_id, $field_name, $group_key, $single_value = false, $value_only = false ) {
// Get the field settings to determine if it's a repeater field.
$wbbpp_profile_fields_settings = get_option( 'wbbpp_profile_fields_settings' );
if ( ! isset( $wbbpp_profile_fields_settings[ $group_key ][ $field_name ] ) ) {
// Field not found in settings.
return '';
}
$field_detail = $wbbpp_profile_fields_settings[ $group_key ][ $field_name ];
$field_meta_key = 'wbbpp_' . $group_key . '_' . $field_name;
$output = '';
if ( isset( $field_detail['repeater'] ) && $field_detail['repeater'] ) {
// Handle repeater fields.
$field_count = get_user_meta( $user_id, $field_meta_key . '_count', true );
$field_count = ( ! empty( $field_count ) ) ? $field_count : 1;
$values = [];
for ( $i = 0; $i < $field_count; $i++ ) {
$meta_key = $field_meta_key . ( $i > 0 ? '_' . $i : '' );
$value = get_user_meta( $user_id, $meta_key, true );
if ( ! empty( $value ) ) {
$values[] = $value;
}
}
if ( $single_value && ! empty( $values ) ) {
// If only a single value is requested, return the first one.
$output = esc_html( $values[0] );
} elseif ( ! empty( $values ) ) {
// Otherwise, return all values as a comma-separated list.
$output = implode( ', ', array_map( 'esc_html', $values ) );
}
} else {
// Handle single-value fields.
$value = get_user_meta( $user_id, $field_meta_key, true );
if ( ! empty( $value ) ) {
$output = esc_html( $value );
}
}
if ( $value_only ) {
// Return only the value(s) without HTML wrapping.
return $output;
} else {
// Return the value(s) wrapped in HTML.
return '<div class="profile-field ' . esc_attr( $field_name ) . '">' . esc_html( $field_detail['field_tile'] ) . ': ' . $output . '</div>';
}
}
/**
* Shortcode to display specific BuddyPress profile field.
*
* @param array $atts Shortcode attributes.
* @return string The HTML or plain text output for the field.
*/
function wbcom_designs_profile_field_shortcode( $atts ) {
// Extract shortcode attributes.
$atts = shortcode_atts(
array(
'user_id' => bp_displayed_user_id(), // Default to displayed user if not provided.
'field_name' => '',
'group_key' => '',
'single_value' => 'false', // Whether to display only the first value of a repeater.
'value_only' => 'false', // Whether to display just the value without any HTML.
),
$atts,
'wbcom_profile_field'
);
// Ensure that both field_name and group_key are provided.
if ( empty( $atts['field_name'] ) || empty( $atts['group_key'] ) ) {
return '<p>Field name and group key are required.</p>';
}
// Convert string "true"/"false" to boolean.
$single_value = filter_var( $atts['single_value'], FILTER_VALIDATE_BOOLEAN );
$value_only = filter_var( $atts['value_only'], FILTER_VALIDATE_BOOLEAN );
// Call the function to display the profile field.
return wbcom_designs_display_profile_field( $atts['user_id'], $atts['field_name'], $atts['group_key'], $single_value, $value_only );
}
// Register the shortcode with WordPress.
add_shortcode( 'wbcom_profile_field', 'wbcom_designs_profile_field_shortcode' );
Summary
This shortcode provides a flexible way to display BuddyPress profile fields on your WordPress site. You can customize how the data is displayed, including options for handling repeater fields and choosing whether to output just the value or a full HTML structure.
