Filter: name parts

Hook
peepso_get_name_parts
Plugin PeepSo Foundation
Since 1.8.2
Status Active
Args
array

Description #

This filter is used to gather and order “parts” used to display the user’s name. The default “parts” are: the real name (first name & last name), or username, depending on availability of the data and the settings.

Practical uses of this filter can include: changing the order of the name parts, adding a middle name or a nickname, removing last names from display etc.

When working with this filter, always assume you will have access to one or more parts, never assume a specific number.

For example, admin or user might decide to only show username (by using the “display name style” settings). Sometimes the real name has not been provided. In these cases the username will be available.

Sometimes the real name is used, but last name is missing – then only the first name will be available.

Limitations #

Do not use this filter to add rich HTML to name parts. This should be done with actions: peepso_action_render_user_name_before and peepso_action_render_user_name_after.

Examples #

1 2 3 4 5 6 7 8 9 10 11 12 13
/**
* In some contexts, the administrator might prefer the last name to be displayed before the first name. 
* In this case, we can reverse the array of the parts
*/
add_filter('peepso_get_name_parts', function($parts, $id) {

    // If there are exactly 2 parts
    if(count($parts) == 2) {
        $parts = array_reverse($parts);
    }

    return $parts;
},10,2);

1 2 3 4 5 6 7 8 9 10 11 12
/**
* Shorten the last names only to the first letter (for example to save space).
*/

add_filter('peepso_get_name_parts', function($parts, $id) {
    // Only if last name is set
    if(isset($parts[1])) {
        $parts[1] = $parts[1][0].'.';
    }

    return $parts;
},10,2);

What are your feelings
Updated on Sep 22, 2023