BuddyDev

Search

Display Name Options

  • Participant
    Level: Enlightened
    Posts: 24
    la on #40279

    I want to allow the user to choose which displays as the display name:
    First Name
    First + Last
    Nickname
    Company Name
    On a business directory website, the default options (1) don’t allow for company name display and (2) allow the user to choose which displays. I want employers to be able to display their Company Name and job-seekers to be able to display their choice of the other options. Is this do-able?

  • Keymaster
    (BuddyDev Team)
    Posts: 19487
    Brajesh Singh on #40291

    Hi,
    Thank you for the question.

    Is Company Name an xprofile field? It is possible to do so by adding a filter on ‘bp_core_get_user_displayname’ but I am afraid it may not be consistent across the themes.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 24
    la on #40367

    Hi,
    Thank you for the response!

    In other words, did I create a field in the fields option of BB settings? If so, yes. So when they register, if they are a company, they have a field for Company Name.

    I’ve been looking at these options but not sure what to change:

    From a previous forum topic:
    https://buddypress.org/support/topic/how-to-set-display_name-xprofile-custom_field-at-the-user-registration/

    add_action( ‘bp_core_activated_user’, ‘change_user_defaults’, 99, 3 );
    function change_user_defaults($user_id, $key, $user) {

    if (empty($user_id)) {
    $user_id = bp_loggedin_user_id();
    }

    if (empty($user_id)) {
    return false;
    }

    $mv_first_name = xprofile_get_field_data(26, $user_id);
    $mv_last_name = xprofile_get_field_data(11, $user_id);
    $name = $mv_first_name . ‘ ‘ . $mv_last_name;

    if ($name !== ‘ ‘) {
    $args = array(
    ‘ID’ => $user_id,
    ‘display_name’ => $name
    );
    wp_update_user($args);
    }
    }

    ______

    Or
    http://hookr.io/functions/bp_get_member_name/

    _________

    Or
    https://www.buddyboss.com/resources/reference/functions/bp_get_member_name/

    function bp_get_member_name() {
    global $members_template;

    // Generally, this only fires when xprofile is disabled.
    if ( empty( $members_template->member->fullname ) ) {
    // Our order of preference for alternative fullnames.
    $name_stack = array(
    ‘display_name’,
    ‘user_nicename’,
    ‘user_login’
    );

    foreach ( $name_stack as $source ) {
    if ( !empty( $members_template->member->{$source} ) ) {
    // When a value is found, set it as fullname and be done with it.
    $members_template->member->fullname = $members_template->member->{$source};
    break;
    }
    }
    }

    $list_fields = bp_xprofile_get_hidden_fields_for_user( $members_template->member->ID, bp_loggedin_user_id() );
    if ( empty( $list_fields ) ) {
    $full_name = $members_template->member->fullname;
    } else {
    $last_name_field_id = bp_xprofile_lastname_field_id();
    if ( in_array( $last_name_field_id, $list_fields ) ) {
    $last_name = $members_template->member->fullname;
    $full_name = str_replace( ‘ ‘ . $last_name, ”, $members_template->member->fullname );
    } else {
    $full_name = $members_template->member->fullname;
    }
    }

    /**
    * Filters the display name of current member in the loop.
    *
    * @since BuddyPress 1.2.0
    *
    * @param string $fullname Display name for current member.
    */
    return apply_filters( ‘bp_get_member_name’, trim( $full_name ) );
    }

    Seems like that’s around the topic but I don’t know what to tweak. I do have Snippets installed so easy to drop it in if I could get some help with the code.

    Thank you so much in advance! Surely other people want to be able to use a profile field and Display Name?

    What would be even better is if:
    IF there is a company name, then display that (meta field from xprofile field), and if not let the user choose from First Name, First/Last or nickname. Is that possible?

You must be logged in to reply to this topic.

This topic is: not resolved