Skip to:
Content
Pages
Categories
Search
Top
Bottom

User Meta Output On Profile


  • Alex Stine
    Participant

    @alexstine

    Hello,
    I have a custom user meta field which is a staff label field. For people with “staff” capability, they can go to their profile in WP admin and enter some text for a label such as “Lead Developer, Writer, Support, etc. I would like to output this label on BBPress profile but cannot figure out how to do it. I am using the currently displayed profile ID. In the following file:
    /wp-content/plugins/buddypress/bp-templates/bp-legacy/buddypress/members/single/members-header.php
    I know it’s bad to hack core files, I will move to my themes folder once it’s working. This is the code I have currently.

    <?php
    $member_id = bp_displayed_user_id();
    if ( user_can( $member_id, 'staff' ) ) {
    if ( get_the_author_meta( 'staff') ) {
    $x = ( get_the_author_meta( 'staff', $member_id ) == '' ) ? "Staff" : get_the_author_meta( 'staff', $member_id );
    echo '<div class="staff-label">';
    echo $x ;
    echo '</div>';
    } 
    } ?>

    This code is working fine in the comments area with different logic to grab the user ID, just not working for BuddyPress. Any suggestions?
    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)

  • Alex Stine
    Participant

    @alexstine

    Any suggestions? Really need to get this working.

    Thanks.


    shanebp
    Moderator

    @shanebp

    Did you try using the meta function?
    https://codex.wordpress.org/Function_Reference/get_user_meta

    And you don’t need user_can.
    Just request the meta and show if it exists.


    Alex Stine
    Participant

    @alexstine

    Hello @shanebp,

    I tried this code but it did not work. Anywhere I went wrong?

    $member_id = bp_displayed_user_id();
    $x = ( get_the_user_meta( 'staff', $member_id ) == '' ) ? "Staff" : get_the_user_meta( 'staff', $member_id );
    echo '<div class="staff-label">';
    echo $x ;
    echo '</div>';

    Thanks.


    shanebp
    Moderator

    @shanebp

    > Anywhere I went wrong?

    Yeah, you didn’t read the codex page.

    Try:

    $staff = get_user_meta( bp_displayed_user_id(), 'staff', true ); 
    if ( ! empty ( $staff ) )
        echo $staff;

    Alex Stine
    Participant

    @alexstine

    Hello

    Thanks, I seemed to misread the codex. I did not realize the word “the” was not included. Here’s the code that did it for me in the end.

    		$member_id = bp_displayed_user_id();
    		$x = ( get_user_meta( $member_id, 'staff', true ) == '' ) ? "Staff" : get_user_meta( $member_id, 'staff', true );
    			echo '<div class="staff-label">';
    			echo $x;
    			echo '</div>';
    		?>

    Thanks for your help @shanebp.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar