Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display specific role on profile user


  • oliviergodefroit
    Participant

    @oliviergodefroit

    Hello,

    I would like to display a badge instead the user role on the user profile.

    I tried many scripts but nothing seems to be good.

    add_action( 'bp_before_member_header_meta', 'role_on_profile' );
     
    function role_on_profile() {
        global $wp_roles;
     
        $user_id = bp_displayed_user_id();
     
        $user = get_userdata($user_id);
     
        $roles = $user->roles;
     
        if ( !$roles )
            return;
     
        if ( !isset( $wp_roles ) )
            $wp_roles = new WP_Roles();
     
        $named_roles = array();
     
        foreach ( $roles as $role ) {
     
            $named_roles [] = $wp_roles->role_names[$role];
        }
     
        if ( isset($named_roles[2]) )
            echo '<span class="user-role activity">'.$named_roles[2].'</span>';
    
    }

    // edited to use CODE button

    // here is the code but it is not working.

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

  • shanebp
    Moderator

    @shanebp

    Please use the code button when posting code.

    BuddyPress does not show the user role on a user profile.

    Can you find out if it is your theme or some plugin that is showing the role?
    That would be the best place to make your change.


    oliviergodefroit
    Participant

    @oliviergodefroit

    My theme don’t show the user role, that is a script to show a badge ( if the user has a specific role).

    This badge will confirm the certified profile.


    shanebp
    Moderator

    @shanebp

    You said the code you posted was not working.
    Now you say it is working?
    But you want some different?
    Please make clear statements and ask clear questions.


    oliviergodefroit
    Participant

    @oliviergodefroit

    Yes excuse me. I would like to dislpay only one specific role on profile page, to certify this user is certified by the moderator.

    here is the code:

    
    add_action( 'bp_before_member_header_meta', 'verifed_profile_check' );
    function verifed_profile_check( ) {
    		
      if ( $role = 'certified_subscribers' ) 
    	echo '<span class="pmpro_label"> CERTIFIED PROFILE </span>';
      }
    

    This code display ‘CERTIFIED PROFILE’ to all profiles but should only display it for users role ‘certified_subscribers’


    shanebp
    Moderator

    @shanebp

    Untested but your solution will be something like this:

    function verifed_profile_check( ) {
    
         $user = get_userdata( bp_displayed_user_id() );
    
         if ( in_array( 'certified_subscribers', (array) $user->roles ) ) {
    
             echo '<span class="pmpro_label"> CERTIFIED PROFILE </span>';
    
         }
    
    }

    oliviergodefroit
    Participant

    @oliviergodefroit

    Hello,

    unfortunately, this code doesn’t work, there is no error message but nothing appear on profiles.


    shanebp
    Moderator

    @shanebp

    Did you include the add_action ?

    add_action( 'bp_before_member_header_meta', 'verifed_profile_check' );
    function verifed_profile_check( ) {
    
         $user = get_userdata( bp_displayed_user_id() );
    
         if ( in_array( 'certified_subscribers', (array) $user->roles ) ) {
    
             echo '<span class="pmpro_label"> CERTIFIED PROFILE </span>';
    
         }
         else { 
              echo '<span class="pmpro_label"> not certified </span>';
         }
    
    }

    The code above should show something. And you or your developer can debug from there.


    oliviergodefroit
    Participant

    @oliviergodefroit

    No i didn’t , i’m so confuse !!!

    That works. This code is OK !

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