BuddyDev

Search

Is there a way to display Custom Xprofile fields using shortcode?

  • Participant
    Level: Enlightened
    Posts: 42
    r08 on #36618

    I am using your custom fields plugin, it’s great!
    https://buddydev.com/plugins/bp-xprofile-custom-field-types/

    Is there a way to display those fields on the website?
    For example, I would like to display one of the fields for a specific user (by entering their user_id in shortcode) and have their profile field show up.

    Some of the fields have multiple checkboxes so would like all those results listed as well.

    Does anyone know if there’s a way to do this?

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

    HI,
    Thank you for the question.

    Do you want to list specific field data of a user or the whole profile(similar to what what is available on BuddyPress profile?)

    Please let me know and I may be able to point in the right direction.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 42
    r08 on #36638

    Yes, I would like to list specific data. Honestly, I’m looking for the ability to choose any field I need and place it on a custom page. Basically, I want to choose several of the members and make them a special Featured Member page. I would custom build the page and add the shortcodes on each page. I might want to display their username and many of the custom fields I created for the BuddyPress Xprofile.

    Found this plugin that does a good job displaying these fields using a shortcode except for one thing. If a field has multiple options it outputs “array” instead of each item – If this worked it would be perfect.
    https://wordpress.org/plugins/bp-xprofile-shortcode/

    I tried this plugin as well but it has a problem that many users are reporting. I’m affected by that problem too and it doesn’t display the custom fields on my site:
    https://wordpress.org/plugins/bp-profile-shortcodes-extra/

    The other thing I need to display is the avatar of a chosen user ID but I understand that has nothing to do with a custom field. I’m still looking for that solution.

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

    Hi,
    Thank you for sharing the details.

    You can remove the plugin and put this code in theme theme’s functions.php

    
    
    /**
     * Xprofile data shortcode.
     */
    add_shortcode( 'bpc-profile-data', function ( $atts = array(), $content = null ) {
    
    	$atts = shortcode_atts(
    		array(
    			'context' => '', // logged, displayed, author
    			'user_id' => 0,
    			'field'   => '',
    		),
    		$atts
    	);
    
    	if ( empty( $atts['field'] ) ) {
    		return '';
    	}
    
    	$user_id = 0;
    	switch ( $atts['context'] ) {
    
    		case 'displayed':
    			$user_id = bp_displayed_user_id();
    			break;
    		case 'logged':
    			$user_id = get_current_user_id();
    			break;
    		case 'author':
    			$user_id = get_the_author_meta( 'ID' );
    			break;
    		default:
    			if ( $atts['user_id'] ) {
    				$user_id = $atts['user_id'];
    			} elseif ( in_the_loop() ) {
    				$user_id = get_the_author_meta( 'ID' );
    			} elseif ( bp_is_user() ) {
    				$user_id = bp_displayed_user_id();
    			} elseif ( is_user_logged_in() ) {
    				$user_id = get_current_user_id();
    			}
    	}
    
    	if ( ! $user_id ) {
    		return '';
    	}
    
    	return xprofile_get_field_data( $atts['field'], $user_id, 'comma' );
    
    } );
    
    

    Now, you can use the shortcode like this

    
    [bpc-profile-data field="About" ]
    

    You can use either the content or the user_id parameter to pass user context or id.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 42
    r08 on #36753
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 42
    r08 on #36755
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 42
    r08 on #36756
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #36757
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 42
    r08 on #37002
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 42
    r08 on #37155
    This reply has been marked as private.

You must be logged in to reply to this topic.

This topic is: not resolved