# BuddyPress: Profile Field Shortcode

> Adds a [wpide_bp_field name="Location"] shortcode that prints a BuddyPress profile field for the logged-in member, or for the profile being viewed.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: buddypress, profile, shortcode
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/buddypress-profile-field-shortcode/

## Code

```php
if ( ! function_exists( 'buddypress' ) ) {
    return;
}

add_shortcode( 'wpide_bp_field', function ( $atts ) {
    $atts    = shortcode_atts( array( 'name' => '', 'user' => 'current', 'fallback' => '' ), $atts, 'wpide_bp_field' );
    $user_id = 'displayed' === $atts['user'] ? bp_displayed_user_id() : bp_loggedin_user_id();

    if ( ! $user_id || '' === $atts['name'] || ! bp_is_active( 'xprofile' ) ) {
        return esc_html( $atts['fallback'] );
    }

    $field_id = xprofile_get_field_id_from_name( $atts['name'] );

    if ( ! $field_id ) {
        return esc_html( $atts['fallback'] );
    }

    // Respect the member's visibility choice when showing someone else's field.
    if ( in_array( (int) $field_id, array_map( 'intval', (array) bp_xprofile_get_hidden_fields_for_user( $user_id, bp_loggedin_user_id() ) ), true ) ) {
        return esc_html( $atts['fallback'] );
    }

    $value = xprofile_get_field_data( $field_id, $user_id, 'comma' );

    return $value ? wp_kses_post( $value ) : esc_html( $atts['fallback'] );
} );
```

## Notes

Add user="displayed" to show the field of the profile being viewed instead of the logged-in member.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "BuddyPress: Profile Field Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
