PHPBB 3.1 Change Display Name

After spending hours trying to change the display name in PHPBB 3.1 so you could should a different name to the username, I finally figured out how to do it.

You have to directly modify PHPBB so remember this in the future if you do any upgrades.

For this to work I created a custom profile field called display_name (you can change this if you wish), this is where a user can modify there name.

Now the bit you need to modify.

Find the file functions_content.php (its in the includes directory).

On line 1355 there is a function called get_username_string.

Add the following bit of code after the global bit.

  
        
global $phpbb_container;

if(!empty($user_id))
 {
 $cp = $phpbb_container->get('profilefields.manager');
 $userProfileFields = $cp->grab_profile_fields_data($user_id);
 if(!empty($userProfileFields[$user_id]["display_name"]["value"]))
  {
  $username = $userProfileFields[$user_id]["display_name"]["value"];
  }
 }
        
  
This basically replaces the username on most pages with the one in display_name if it is set.

13 comments:

  1. Awesome this did the trick!!! Screw F'ing with templates!

    ReplyDelete
  2. We have LDAP enable on our Active Directory so I expanded on the example you posted.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. // replace username with custom field displayname START
    if(!empty($user_id))
    {
    $cp = $phpbb_container->get('profilefields.manager');
    $userProfileFields = $cp->grab_profile_fields_data($user_id);

    if(!empty($userProfileFields[$user_id]["displayname"]["value"]))
    {
    $username = $userProfileFields[$user_id]["displayname"]["value"];
    }
    else
    {
    $username = getldapname($username);
    if (!empty($username)) {
    $cp_data = array('pf_displayname' => $username);
    $cp->update_profile_field_data($user_id, $cp_data);
    }
    }
    }
    // replace username with custom field displayname END

    ReplyDelete
  5. FYI, I used displayname instead of display_name.
    Then added a function to pull the canonical name or cn from AD via LDAP.

    ReplyDelete
  6. function getldapname($username) {
    $ldapDomian = 'DC=domain,DC=com';

    $ntDomain = 'LOCAL.COM\\';

    // $adServer is holds the AD SERVER address running
    // LDAP directory service this can also be an IP address
    $adServer = "ldap://domaincontroller.local.com";

    // This is where the server connection happens
    $ldap = ldap_connect($adServer);

    $ldaprdn = $ntDomain . "ldapuser";
    $password = "ldapuserpassword";

    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

    // This is where the Authentication happens
    $bind = @ldap_bind($ldap, $ldaprdn, $password);

    $userDn="";

    if ($bind) {
    // Filter LDAP objectClass for "user & person", the "sAMAccountName" to equal the $usename varible
    // and "UserAccountControl" for active accounts only
    $filter="(&(objectClass=user)(objectCategory=Person)(sAMAccountName=$username)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))";

    $result = ldap_search($ldap,$ldapDomian,$filter);
    ldap_sort($ldap,$result,"sn");
    $info = ldap_get_entries($ldap, $result);
    for ($i=0; $i<$info["count"]; $i++)
    {
    if($info['count'] > 1)
    break;
    $userDn = $info[$i]["cn"][0];
    }
    @ldap_close($ldap);
    }

    return $userDn;
    }

    ReplyDelete
  7. The above code pull the name via LDAP and updates the custom "displayname" field.

    ReplyDelete
  8. This is exactly what I am looking for however it brings one problem. When I am in the ACP and I want to manage a user, I would normally click the button to find a user (Which brings up the user list) the issue is when I click on a user their it fills in the custom name not their actual username giving an error that said user could not be found.

    ReplyDelete
  9. Why edit core code when there is a PHP event for just this very thing? Edits will be lost on update if overwriting files.

    ReplyDelete
  10. I found the get_username on line 1452 on verison 3.1.10

    ReplyDelete
  11. Working perfectly for me...

    ############

    global $user, $auth;

    /** Begin replace username with screen_name for display /*toggle on/off*/
    global $phpbb_container;
    if(!empty($user_id)) {
    $cp = $phpbb_container->get('profilefields.manager');
    $userProfileFields = $cp->grab_profile_fields_data($user_id);
    if(!empty($userProfileFields[$user_id]["screen_name"]["value"]))
    { $username = $userProfileFields[$user_id]["screen_name"]["value"]; } }
    /** End replace username with screen_name for display */

    // This switch makes sure we only run code required for the mode
    switch ($mode)

    ############

    Many thanks!

    ReplyDelete
  12. helow i have phpBB3.2 i use ldap unth and Change Display Name insted of username
    my email: skyforce_2030@hotmail.com

    ReplyDelete