So I came up with two solutions of my own.
SOLUTION #1: The simplier one, I implemented from a WordPress plugin using hook_civicrm_contact_get_displayname
but you could also impement it from within a civicrm plugin as well. This is fairly simple code and will add the nickname (if it exists) to the end of the display name, everywhere the display name is shown. Here is wordpress plugin code:
require_once(ABSPATH . 'wp-includes/pluggable.php');
add_filter( 'civicrm_contact_get_displayname', 'my_customhook_civicrm_contact_get_displayname', 10, 4 );
function my_customhook_civicrm_contact_get_displayname(&$display_name, $contactId, $dao) {
$nick_name = trim($dao->nick_name);
if ($nick_name != "" && ! is_null($nick_name) ) {
$display_name .= " [$nick_name]";
}
}
Here is what solution #1 looks like:
SOLUTION #2: Is the more complicated one, using the buildForm hook from within a civicrm plugin. It will only display the nickname at the end of the display name within the View Contact Summary pages (at the top of the page). It has the advantage of being able to style the nickname portion different from the rest of the display name so you can make it stand out or be less obtrusive. Here is civicrm plugin code:
function my_customfunctions_civicrm_buildForm($formName, &$form) {
if ($formName == "CRM_Activity_Form_ActivityLinks") {
if (strpos($_SERVER['QUERY_STRING'], "page=CiviCRM") !== false && strpos($_SERVER['QUERY_STRING'], "q=civicrm%2Fcontact%2Fview") !== false) { // This checks to make sure its the Contact View page only that I am modifying.
// Add javascript to the page.
// Need to use +String.fromCharCode(39)+ to wrap span id in single-quotes.
// Need to check if the Nickname was already added, otherwise it gets added more than once.
CRM_Core_Region::instance('page-body')->add( ['script' => "nicknode = document.getElementsByClassName('crm-summary-display_name')[0]; nicknode_text = nicknode.textContent; nickname = document.getElementsByClassName('crm-content crm-contact-nick_name')[0]; nickname_text = nickname.textContent; if (nicknode_text.length > 0 && nickname_text.length > 0) { if (nicknode_text.includes(nickname_text) == false) { nicknode.innerHTML = nicknode_text + ' <span id='+String.fromCharCode(39)+'civicrm-customjs-nickname'+String.fromCharCode(39)+' >[' + nickname_text + ']</span>'; } }" ] );
// Add some CSS for the newly added span tag to style as-needed.
CRM_Core_Region::instance('page-body')->add( ['script' => "cj('#civicrm-customjs-nickname').css('font-size', '75%'); cj('#civicrm-customjs-nickname').css('font-style', 'italic');"] );
}
}
}
Here is what solution #2 looks like (you can style the nickname portion):
Of course I came up with the 2nd more complicated solution first, before finding the simpler solution #1.