I know this question was asked a long time ago, but I wanted to post an alternative. It looks like you want to change the field in the $ variables array, which $variables['name'] matches what you have in the custom field that I called field_real_name . If you use the preprocess function, there is no need to retract the global $user object. You have access to the $variables array, so you can get information about the user with this - it will load the information related to uid ( see Template_preprocess_username ):
function mythemename_preprocess_username(&$variables) { $account = user_load($variables['account']->uid); ...more code will go here in a moment }
If you are dpm($account) (or kpr($account) , if you are not using devel), you will see that you have access to all user information without using the global $user object.
Then you can change the output of $variables['name'] as your field_real_name as follows:
function mythemename_preprocess_username(&$variables) { // Load user information with user fields $account = user_load($variables['account']->uid); // See if user has real_name set, if so use that as the name instead $real_name = $account->field_real_name[LANGUAGE_NONE][0]['safe_value']; if (isset($real_name)) { $variables['name'] = $real_name; } }
source share