Skip to:
Content
Pages
Categories
Search
Top
Bottom

User profile link, only show username.


  • joshnoworries
    Participant

    @joshnoworries

    How can i change bbPress to only show the ‘username’ instead of Nickname/First Last name?

    I found some information, but it only allows changing type to name/avatar. With no control over the name being “Display name, Nickname, Username”: http://codex.bbpress.org/bbp_author_link/

    <?php bbp_author_link( array( ‘post_id’ => bbp_get_topic_last_active_id(), ‘size’ => 14 ) ); ?>

    I am using “BP Display Name” to do this on BuddyPress pages, and have hardcoded it on other templates. But just need to sort it on the bbPress forum to make it consistent across the site.

    WP version: 3.7.1
    bbPress version: 2.4.1
    Site: http://www.veggie.co.nz/forums/forum/introductions/

    Thanks very much for your help.

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

  • Robin W
    Moderator

    @robin-w

    This may look a bit messy but without spending a lot of time

    In loop-single-reply.php (wp-content/plugins/bbpress/templates/default/bbpress)

    change line 45 which uses bbp_reply_author_link

    and use

    bbp_get_reply_author_avatarto display the avatar and

    bbp_get_reply_author_id
    to get the id – you’ll then need to play with maybe

    bbp_get_reply_author()
    putting the id in the brackets

    eg bbp_get_reply_author(bbp_get_reply_author_id )

    should give you the username rather than display name

    you’d then need to play with

    content-single-topic.php for the forum lists (wp-content/plugins/bbpress/templates/default/bbpress)


    justsayno1
    Participant

    @justsayno1

    Hi,

    I am in the same situation and I have found a solution. You can apply filters to almost every function in bbpress it seems. I just followed the functions until I found these three filters:

    apply_filters( 'bbp_get_user_profile_link', $user_link, $user_id );
    apply_filters( 'bbp_get_reply_author_display_name', $author_name, $reply_id )
    apply_filters( 'bbp_get_topic_author_link', $author_link, $args );
    

    Using these you can change basically everything. I found it easier to change the avatar ALT text vie the normal wordpress get_avatar filter since I wanted that to happen site wide anyway. Here is an example of the reply filter:

    
    function replace_bbpress_replies_username_filter($author_name,$reply_id ){
    $author_id = bbp_get_reply_author_id($reply_id);
    $author_object = get_userdata( $author_id );
    $author_name  = ucfirst($author_object->user_login);
    return $author_name;
    }
    add_filter( 'bbp_get_reply_author_display_name','replace_bbpress_replies_username_filter',10, 2);
    

    Hope that helps. You can do it through templating but this way you don’t have to worry about doing it in a million different templates and then also dealing the layout of the markup you use to replace the current markup. And if filters confuse you and you don’t understand go read this:

    http://dev.themeblvd.com/tutorial/filters/

    I had zero knowledge of how to do filters when I read this thread 16 hours ago and decided to not go the route the above person mentioned.

    EDIT: I thought I’d add that they really could make this a lot easier with very little effort and make it so that you could configure what to use as a display name…. If anyone reads this from bbPress I’d be happy to get involved and do it for them. Seems so dumb to default to make it peoples full names when 90% people don’t want that. That is what facebook, G+ etc is for.


    Robin W
    Moderator

    @robin-w

    @justsayno1

    I just followed the functions until I found these three filters: 🙂

    Ok, so given that loop-single-reply has

    bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => false ) );

    How did you get from there to “I just followed…”?

    I’ve never got into this area (have only been playing with wordpress since March this year, so am still a newbie), and would love to, but can’t find any tutorials that get you from a line on a display php to the backend function that creates it. I understand how a filter works (and that link is the best explanation I’ve seen so far!), but have you got a logical way to then backtrack to find out what you’re going to create a filter for? – I’ve looked through bbpress and there are lots of “function.php”s, the most obvious being bbpress/includes/common/functions.php, but a search for ‘bbp_get_user_profile_link’ comes up blank.

    If you could explain how you get from a to b, I be really grateful !


    joshnoworries
    Participant

    @joshnoworries

    Thanks for all the suggestions! I will investigate the filter as that would be much more elegant. Here is what i am currently using, it’s ugly but i needed an urgent solution for a client:

    loop-single-forum.php

    REPLACED:
    <?php bbp_author_link( array( 'post_id' => bbp_get_forum_last_active_id(), 'size' => 14 ) ); ?>

    WITH:

    <?php if(bbp_get_forum_last_active_id()){?>
    <?php $last_post_id = bbp_get_forum_last_active_id(bbp_get_forum_last_topic_id()); ?>
    <?php $last_user_id = bbp_get_topic_author_id($last_post_id); ?>
    <a href="<?php echo bbp_topic_author_url($last_post_id) ?>"><?php bbp_topic_author_avatar($last_post_id,15); ?>
    <?php $user = get_user_by( 'id',$last_user_id ); echo $user->user_login ; ?></a>
    <?php } else { ?>
    <?php } ?>

    loop-single-topic.php

    REPLACED:

    <?php printf( __( 'Started by: %1$s', 'bbpress' ), bbp_get_topic_author_link( array( 'size' => '14' ) ) ); ?>
    

    WITH:
    Started by: <a href="<?php echo bbp_topic_author_url() ?>"><?php bbp_topic_author_avatar(0,15); ?><?php $user = get_user_by( 'id',bbp_get_topic_author_id() ); echo $user->user_login ; ?></a>

    REPLACED:
    <?php bbp_author_link( array( 'post_id' => bbp_get_topic_last_active_id(), 'size' => 14 ) );?>

    WITH:

    <?php $last_post_id = bbp_get_topic_last_active_id() ?>
    <?php $last_user_id = bbp_get_topic_author_id($last_post_id) ?>
    <a href="<?php echo bbp_topic_author_url($last_post_id) ?>"><?php bbp_topic_author_avatar($last_post_id,15); ?>
    <?php $user = get_user_by( 'id',$last_user_id ); echo $user->user_login ; ?></a>

    loop-single-reply

    REPLACED:
    <?php bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => true ) ); ?>

    WITH:

    <a href="<?php echo bbp_reply_author_url() ?>"><?php bbp_reply_author_avatar(0,80); ?><br/>
    <?php $user = get_user_by( 'id',bbp_get_reply_author_id() ); echo $user->user_login ; ?></a>
    <?php bbp_reply_author_role() ?>

    justsayno1
    Participant

    @justsayno1

    How did you get from there to “I just followed…”?

    Unfortunately there is not an easy way to explain this as I am not that familair with wordpress/bbpress core myself. BUT what I mean is that function is obviously somewhere in the bbpress core files. All I did was start going in there. They are quite well documented and have comments that explain the different functions that get used. You are mainly looking for the template tags function files. Also most plugins/php apps use an includes folder to put all the main crux of the things in. While the main file is just simply a link to all those areas an common functions. If you really can’t find it then just use a program with a “search in files” option and just try and find it that way. I think that is what I did to start with.

    For example when I wanted to find the function yo mention above:
    bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => false ) );

    I thought about what the function is saying and where in the folder directories of includes it may be. Since it was “reply” related I figured it would be in the reply section. I just used some common sense to figure out the template-tags.php probably containted template specific functions. Then I just did a search on those files until I found the bbp_reply_author_link. Which then calls bbp_get_reply_author_link which in tern as a return value of:

    return apply_filters( 'bbp_get_reply_author_link', $author_link, $r );

    Meaning that you can basically sub in your own functino for this function by using:

    add_filter( 'bbp_get_reply_author_display_name', 'replace_bbpress_replies_username_filter', 10, 2);

    Where replace_bbpress_replies_username_filter is your replacement filter. Then what I mean by following the functions is that i realised that this function was fine. It was the bpp_get_author_display_name that I was unhappy with. And this would be called in many places (presumably). So I overwrote that function. EG:

    
    function replace_bbpress_replies_username_filter($author_name,$reply_id ){
    $author_id = bbp_get_reply_author_id($reply_id);
    $author_object = get_userdata( $author_id );
    $author_name  = ucfirst($author_object->user_login);
    return $author_name;
    }
    add_filter( 'bbp_get_reply_author_display_name', 'replace_bbpress_replies_username_filter', 10, 2);
    

    Does that help? I know it seems daunting but getting into the core files and seeing what they are doing is I have found the best way when you are thinking “what the fuck!”. Also bear in mind I spend about 4 hours yesterday figuring this stuff out (but I didn’t understand filters really until now). Just to help you out futher. The three filters I added were as follows:

    
    add_filter( 'bbp_get_reply_author_display_name', 'replace_bbpress_replies_username_filter', 10, 2);
    add_filter( 'bbp_get_topic_author_display_name', 'replace_bbpress_topic_author_display_name', 10, 2);
    add_filter( 'bbp_get_user_profile_link', 'replace_bbpress_profile_link_text_filter', 10, 2 );
    

    The display name function I have shown you. The topic display name function:

    
    function replace_bbpress_topic_author_display_name( $topic_id = 0 ) {
    		$topic_id = bbp_get_topic_id( $topic_id );
    
    		// Check for anonymous user
    		if ( !bbp_is_topic_anonymous( $topic_id ) ) {
    
    			// Get the author ID
    			$author_id = bbp_get_topic_author_id( $topic_id );
    			$author_name = get_the_author_meta( 'user_login', $author_id );
    
    		// User does not have an account
    		} else {
    			$author_name = get_post_meta( $topic_id, '_bbp_anonymous_name', true);
    		}
    
    		// If nothing could be found anywhere, use Anonymous
    		if ( empty( $author_name ) )
    			$author_name = __( 'Anonymous', 'bbpress' );
    
    		// Encode possible UTF8 display names
    		if ( seems_utf8( $author_name ) === false )
    			$author_name = utf8_encode( $author_name );
    
    		return $author_name;
    }

    And for the profile link:

    
    function replace_bbpress_profile_link_text_filter($user_link, $user_id){
    		// Validate user id
    		$user_id = bbp_get_user_id( $user_id );
    		$user_object = get_userdata( $author_id );
    		$user_name  = ucfirst($author_object->user_login);
    		if ( empty( $user_id ) )
    			return false;
    
    		$user      = get_userdata( $user_id );
    		$name      = esc_attr( $user_name );
    		$user_link = '<a href="' . bbp_get_user_profile_url( $user_id ) . '" title="' . $name . '">' . $name . '</a>';
    }

    I also changed the default wordpress “get_avatar” function because it returns the gravatar users real name as the alt attr. All you really have to do it replace the function with the same function but get the users login name and replace the alt text name with that. the function is big and I don’t wanna make this post any more ridiculous.

    It’s cool to be the one giving the answers on a WordPress forum! I only really have learned this stuff in the last few months myself. I am more of a Drupal guy. Shock Horror.

    Cheers!


    Robin W
    Moderator

    @robin-w

    Hey, Thanks for a great and detailed response, I’ll read it in detail later as off to earn a living, but I’ll be digging into it and the code tonight !

    Thanks again – really helpful !


    justsayno1
    Participant

    @justsayno1

    No worries, I like putting up answers like this because people have done the same for me and it was a life saver. Also I feel like this is something that bbpress should just be able to do through the admin panel so I thought I would help out any one who wants to not expose their users real names to everybody.


    Mr-B
    Participant

    @mr-b-2

    For us numberless masses that want to implement exactly what you have, i.e. displaying nickname instead of fullname, without learning the whole product, could this simply be a procedure?
    e.g.
    1: Edit file xxx and replace with these lines:
    blah 1
    blah 2
    2: Edit file ABC and replace DEF with GHI

    Pretty please with sugar on top! 🙂


    Robin W
    Moderator

    @robin-w

    Mr-B,

    If I get a moment in the next few days, I’ll fork all that code into a plugin for you/others.


    Mr-B
    Participant

    @mr-b-2

    Ooh yeah – I would be first in line to install that!
    I do like bbPress, but it seems like something that should be built in. Anyway, thanks, this effort is appreciated.


    Robin W
    Moderator

    @robin-w

    ok, I’ve cut justsayno1’s code into a plugin, which you can download from here

    This will then override any user choice, and enforce only username as the display

    bbpress username plugin

    Download this to your PC, and then using dashboard>plugins>add new and select upload files and upload the zipped file to your site

    Let me know what works and doesn’t

    Three things :

    1. As justsayno1 says – the avatar will display the “display name” as the “alt” choice.
    2. The profile page still appears to have the “display name”
    3. I haven’t yet worked out how to take out the user choice to edit what is displayed, even though of course that is now redundant ie doesn’t work


    @Mr-B
    – give it a go and let me know what works and what doesn’t – I basically just nicked Justsayno’s code.


    @Justsayno1
    on No. 1 above, can you point me to where the get-avatar function is in main wordpress (the file name would be great) and I’ll amend and cut that code into the plugin as well


    @Justsayno1
    on No.s 2 & 3 – have you fixed these, and if so can you point me to the solutions, and again I’ll add then to the plugin.


    Mr-B
    Participant

    @mr-b-2

    Works for me
    http://www.engdex.pl/topic/that-wartburg/
    High fives for that – It’s better than the full name for sure, but wouldn’t it better to show the nickname?


    Mr-B
    Participant

    @mr-b-2

    I might have been too hasty, there is a problem. At the drilldown level the display is fine, when sitting in the subcategory you can see the correct gravatar and short name “kiwib”:
    http://www.engdex.pl/forum/engdex/kbase/

    But looking from the top level, it seems to be showing my gravatar, but with a different user name, “akwadmin”.
    http://www.engdex.pl/forums/

    Can anyone help?
    Brian

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