Skip to:
Content
Pages
Categories
Search
Top
Bottom

Change the color of individual role names


  • priom
    Participant

    @priom

    Hi,

    I’ve recently just switched my site over to bbpress from phpbb3. Currently love how i can style it alot better than phpbb3.

    The only option I would really like at this moment is to have each roles name’s be in different color.

    I know of the bbp-author-name in css but that changes all the author’s name colors. I would basically like a Keymaster to have lets say Green, Moderator to have Yellow, and Participant to have White.

    Something similar to that. Is this possible at this stage?

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

  • Robkk
    Moderator

    @robkk

    you add this to your themes functions.php or a functionality plugin.

    you then use css to style the roles from there.

    add_filter('bbp_before_get_reply_author_role_parse_args', 'ntwb_bbpress_reply_css_role' );
    function ntwb_bbpress_reply_css_role() {
    
    	$role = strtolower( bbp_get_user_display_role( bbp_get_reply_author_id( $reply_id ) ) );
    	$args['class']  = 'bbp-author-role bbp-author-role-' . $role;
    	$args['before'] = '';
    	$args['after']  = '';
    
    	return $args;
    }
    
    add_filter('bbp_before_get_topic_author_role_parse_args', 'ntwb_bbpress_topic_css_role' );
    function ntwb_bbpress_topic_css_role() {
    
    	$role = strtolower( bbp_get_user_display_role( bbp_get_topic_author_id( $topic_id ) ) );
    	$args['class']  = 'bbp-author-role bbp-author-role-' . $role;
    	$args['before'] = '';
    	$args['after']  = '';
    
    	return $args;
    }

    It grabs the topic or reply author role and adds another CSS class with prefix bbp-role- so you now have a new set of CSS classes e.g. bbp-role-keymaster, bbp-role-moderator, bbp-role-participant etc that you can then add your custom CSS styles to.

    link this was from

    Topic background color depending on role


    priom
    Participant

    @priom

    Thanks for the response!

    Unfortunately I did try that and it didn’t work 🙁

    Ah well, guess I’ll just have to live without it. Basically was trying to make it a bit more nicer and similar to phpbb3’s feature in a way. It’s mainly for a gaming guild website but so far everything else looks good enough 🙂

    Loving the forums.


    Robkk
    Moderator

    @robkk

    you add this to your themes functions.php or a functionality plugin.

    you then use css to style the roles from there.

    add_filter('bbp_before_get_reply_author_role_parse_args', 'ntwb_bbpress_reply_css_role' );
    function ntwb_bbpress_reply_css_role() {
    
    	$role = strtolower( bbp_get_user_display_role( bbp_get_reply_author_id( $reply_id ) ) );
    	$args['class']  = 'bbp-author-role bbp-author-role-' . $role;
    	$args['before'] = '';
    	$args['after']  = '';
    
    	return $args;
    }
    
    add_filter('bbp_before_get_topic_author_role_parse_args', 'ntwb_bbpress_topic_css_role' );
    function ntwb_bbpress_topic_css_role() {
    
    	$role = strtolower( bbp_get_user_display_role( bbp_get_topic_author_id( $topic_id ) ) );
    	$args['class']  = 'bbp-author-role bbp-author-role-' . $role;
    	$args['before'] = '';
    	$args['after']  = '';
    
    	return $args;
    }

    It grabs the topic or reply author role and adds another CSS class with prefix bbp-role- so you now have a new set of CSS classes e.g. bbp-role-keymaster, bbp-role-moderator, bbp-role-participant etc that you can then add your custom CSS styles to.

    link this was from

    Topic background color depending on role

    more info about a functionality plugin

    How to create your own WordPress functionality plugin


    Robkk
    Moderator

    @robkk

    @priom

    it worked when i tryed it

    remember to make sure you add the right css

    like for example for the keymaster role use this

    .bbp-author-role-keymaster {
    background:blue;
    color:white;
    padding:0 5px;
    }

    Skisma
    Participant

    @skisma

    @robkk

    Is this to change the background color of the users reply or his actual name? I would like the same for my forum. I would like to change the colors of staff members as well as bold their names.


    Robkk
    Moderator

    @robkk

    @skisma

    this topic is for changing the role names background color.

    like for example this site has them with a gray background

    if i use the code snippet above i can have blue for participants , red for keymasters , and yellow for moderators and so on for each role just by applying a little CSS.

    make a new topic and ill check out doing that in the next few days when i setup my custom cpu up all right with a local dev area and not use linux anymore.


    Skisma
    Participant

    @skisma

    @robkk

    OK will do, I’ll create a new topic and tag you in it. Thanks!


    p1s4
    Participant

    @p1s4

    I know this topic is very old, but is the most recent i found about this argument.

    I need different colors for authors link ( topic and replys) .

    I solved using the filters bbp_get_reply_author_link and bbp_get_topic_author_link in function.php of my child theme.

    I changed the class $link_class in $author_link, you can found the original code in bbpress/includes/topics/template.php and bbpress/includes/reply/template.php.

    This is the complete code in my function.php child’s theme:

    //custom color LINK AUTHOR roles
    add_filter( 'bbp_get_reply_author_link', 'addrole_reply_author_links', 10, 2);
    add_filter( 'bbp_get_topic_author_link', 'addrole_topic_author_links', 10, 2);
    function addrole_topic_author_links($author_link, $args) {
    // Parse arguments against default values
    		$r = bbp_parse_args( $args, array(
    			'post_id'    => 0,
    			'link_title' => '',
    			'type'       => 'both',
    			'size'       => 80,
    			'sep'        => ' ',
    			'show_role'  => false
    		), 'get_topic_author_link' );
    
    		// Used as topic_id
    		if ( is_numeric( $args ) ) {
    			$topic_id = bbp_get_topic_id( $args );
    		} else {
    			$topic_id = bbp_get_topic_id( $r['post_id'] );
    		}
    
    		// Topic ID is good
    		if ( !empty( $topic_id ) ) {
    
    			// Get some useful topic information
    			$author_url = bbp_get_topic_author_url( $topic_id );
    			$anonymous  = bbp_is_topic_anonymous( $topic_id );
    
    			// Tweak link title if empty
    			if ( empty( $r['link_title'] ) ) {
    				$link_title = sprintf( empty( $anonymous ) ? __( 'View %s\'s profile', 'bbpress' ) : __( 'Visit %s\'s website', 'bbpress' ), bbp_get_topic_author_display_name( $topic_id ) );
    
    			// Use what was passed if not
    			} else {
    				$link_title = $r['link_title'];
    			}
    
    			// Setup title and author_links array
    			$link_title   = !empty( $link_title ) ? ' title="' . esc_attr( $link_title ) . '"' : '';
    			$author_links = array();
    
    			// Get avatar
    			if ( 'avatar' === $r['type'] || 'both' === $r['type'] ) {
    				$author_links['avatar'] = bbp_get_topic_author_avatar( $topic_id, $r['size'] );
    			}
    
    			// Get display name
    			if ( 'name' === $r['type'] || 'both' === $r['type'] ) {
    				$author_links['name'] = bbp_get_topic_author_display_name( $topic_id );
    			}
    
    			// Link class
    			//ADD ROLE CLASS TO LINK 
    			$role        = str_replace(' ', '-', strtolower(bbp_get_user_display_role( bbp_get_topic_author_id( $topic_id )) ));
    			$link_class = ' class="'.$role.' bbp-author-' . esc_attr( $r['type'] ) . '"';
    
    			// Add links if not anonymous
    			if ( empty( $anonymous ) && bbp_user_has_profile( bbp_get_topic_author_id( $topic_id ) ) ) {
    
    				$author_link = array();
    
    				// Assemble the links
    				foreach ( $author_links as $link => $link_text ) {
    					//ADD ROLE CLASS TO LINK 
    					$role        =  str_replace(' ', '-', strtolower(bbp_get_user_display_role( bbp_get_topic_author_id( $topic_id )) ));
    					$link_class = ' class="'.$role.'  bbp-author-' . esc_attr( $link ) . '"';
    					$author_link[] = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', esc_url( $author_url ), $link_title, $link_class, $link_text );
    				}
    
    				if ( true === $r['show_role'] ) {
    					$author_link[] = bbp_get_topic_author_role( array( 'topic_id' => $topic_id ) );
    				}
    
    				$author_link = implode( $r['sep'], $author_link );
    
    			// No links if anonymous
    			} else {
    				$author_link = implode( $r['sep'], $author_links );
    			}
    
    		} else {
    			$author_link = '';
    		}
    
    return $author_link;
    }
    
    function addrole_reply_author_links($author_link, $args) {
    
    		// Parse arguments against default values
    		$r = bbp_parse_args( $args, array(
    			'post_id'    => 0,
    			'link_title' => '',
    			'type'       => 'both',
    			'size'       => 80,
    			'sep'        => '&nbsp;',
    			'show_role'  => false
    		), 'get_reply_author_link' );
    
    		// Used as reply_id
    		if ( is_numeric( $args ) ) {
    			$reply_id = bbp_get_reply_id( $args );
    		} else {
    			$reply_id = bbp_get_reply_id( $r['post_id'] );
    		}
    
    		// Reply ID is good
    		if ( !empty( $reply_id ) ) {
    
    			// Get some useful reply information
    			$author_url = bbp_get_reply_author_url( $reply_id );
    			$anonymous  = bbp_is_reply_anonymous( $reply_id );
    
    			// Tweak link title if empty
    			if ( empty( $r['link_title'] ) ) {
    				$link_title = sprintf( empty( $anonymous ) ? __( 'View %s\'s profile', 'bbpress' ) : __( 'Visit %s\'s website', 'bbpress' ), bbp_get_reply_author_display_name( $reply_id ) );
    
    			// Use what was passed if not
    			} else {
    				$link_title = $r['link_title'];
    			}
    
    			// Setup title and author_links array
    			$link_title   = !empty( $link_title ) ? ' title="' . esc_attr( $link_title ) . '"' : '';
    			$author_links = array();
    
    			// Get avatar
    			if ( 'avatar' === $r['type'] || 'both' === $r['type'] ) {
    				$author_links['avatar'] = bbp_get_reply_author_avatar( $reply_id, $r['size'] );
    			}
    
    			// Get display name
    			if ( 'name' === $r['type']   || 'both' === $r['type'] ) {
    				$author_links['name'] = bbp_get_reply_author_display_name( $reply_id );
    			}
    
    			// Link class
    			//ADD ROLE CLASS TO LINK  
    			$role        =  str_replace(' ', '-', strtolower(bbp_get_user_display_role( bbp_get_reply_author_id( $reply_id ) )));
    			$link_class = ' class="'.$role.' bbp-author-' . esc_attr( $r['type'] ) . '"';
    
    			// Add links if not anonymous and existing user
    			if ( empty( $anonymous ) && bbp_user_has_profile( bbp_get_reply_author_id( $reply_id ) ) ) {
    
    				$author_link = array();
    
    				// Assemble the links
    				foreach ( $author_links as $link => $link_text ) {
    					//ADD ROLE CLASS TO LINK 
    					$role        =  str_replace(' ', '-', strtolower(bbp_get_user_display_role( bbp_get_reply_author_id( $reply_id ) )));
    					$link_class = ' class="'.$role.'  bbp-author-' . $link . '"';
    					$author_link[] = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', esc_url( $author_url ), $link_title, $link_class, $link_text );
    				}
    
    				if ( true === $r['show_role'] ) {
    					$author_link[] = bbp_get_reply_author_role( array( 'reply_id' => $reply_id ) );
    				}
    
    				$author_link = implode( $r['sep'], $author_link );
    
    			// No links if anonymous
    			} else {
    				$author_link = implode( $r['sep'], $author_links );
    			}
    
    		// No replies so link is empty
    		} else {
    			$author_link = '';
    		}
    
    		return $author_link;
    	}

    Then you have to add the CSS to the new class added to author’s link.

    Hope will be useful,
    bye,
    Marco.

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