Skip to:
Content
Pages
Categories
Search
Top
Bottom

Rol in cover page of profile

  • @jennifersanchez

    Participant

    Hi!
    I saw this code for add the role in the profile of the User.
    I want show it in the cover page of his profile, can you help me?
    Thanks very much!

    add_action( 'bp_before_member_header_meta', 'devb_show_role_on_profile' );
     
    function devb_show_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 ( $named_roles )
            echo '<span class="user-role activity">' . join(', ', $named_roles) . '</span>';
    }
Viewing 9 replies - 1 through 9 (of 9 total)
  • @venutius

    Moderator

    Hi there, please put code in backticks. Ive edited your post and added them.

    So what is the problem with this?

    @jennifersanchez

    Participant

    Hello! Thank you! I would want that the role of user to be shown on the cover of the page.
    or you want to say that it does and my theme does not interpret it? Can i put this in the header.php?

    @venutius

    Moderator

    No yu’d put this either in your child-themes functions.php or in wp-content/plugins/bp-custom.php, looking at the code it should work.

    @jennifersanchez

    Participant

    Hi Venutio! I modified this

    function devb_show_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 ($named_roles)        return '<span class="user-role activity">' . join(', ', $named_roles) . '</span>';
    }
    add_filter('eo_title_content_html_below_title', 'devb_show_role_on_profile');

    This works and show it in the cover page of the profile.
    the problem is in the other pages like group or other page out of my profile (when i open it) this error appear in the cover page:
    NOTICE: TRYING TO GET PROPERTY ‘ROLES’ OF NON-OBJECT IN

    I tried with !null but it didnt work.
    Any suggest?
    Thanks!!

    @snorklebum

    Participant

    I used very similar code but add_filter('bp_before_member_header_meta', 'devb_show_role_on_profile');

    Do you want it to display on other pages?

    @venutius

    Moderator

    The problem is that you are trying to run this on every page when it will only work on the user profile pages as bp_displayed_user_id() will only work on a user profile page. so you would need to add something like:

     if ( ! bp_is_user_profile() ) {
        return;
    }

    @jennifersanchez

    Participant

    Hi! i tried both options..but it didnt work.
    This is the last version with your suggest, any idea?Thanks!!!!

    function devb_show_role_on_profile()
    {    global $wp_roles;
    $user_id = bp_displayed_user_id();
    $user = get_userdata($user_id);
    $roles = $user->roles;
    $profilebuddy = bp_is_user_profile() ;
    if ( !$profilebuddy )
          return '';
      
    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 ($named_roles)        
    return '<span class="user-role_activity">' . join(', ', $named_roles) . '</span>';
    
            
        }
    
    add_filter('eo_title_content_html_below_title', 'devb_show_role_on_profile');

    @shanebp

    Moderator

    Try deleting this:

    $profilebuddy = bp_is_user_profile() ;
    if ( !$profilebuddy )
          return '';

    And doing this:

    function devb_show_role_on_profile() {
        global $wp_roles;
        
        if ( ! bp_is_user() ) {
            return '';
        } 
        // etc

    You are using this custom filter hook: eo_title_content_html_below_title
    But you do not indicate what that hook is actually filtering.
    IOW: return ''; does not make sense without some context.
    What is the full line for the hook? apply_filters( // something

    @jennifersanchez

    Participant

    Great @shanebp this works!! =)
    these is a custom shortcode from my theme.

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