ulgm_rest_api_display_name_order
Filters the display name order for user profiles when outputting to the REST API.
add_filter( 'ulgm_rest_api_display_name_order', $callback, 10, 3 );
Description
Filters the order of first and last names when constructing the display name for the REST API. Developers can modify the array to change the name order or format it differently. The original first and last names are passed as separate arguments for more granular control.
Usage
add_filter( 'ulgm_rest_api_display_name_order', 'your_function_name', 10, 3 );
Parameters
-
$first_name(mixed) - This parameter contains the user's first name.
-
$first_name(mixed) - This parameter contains the user's first name.
-
$last_name(mixed) - This parameter is used to pass the user's last name to the filter.
Return Value
The filtered value.
Examples
/**
* Example filter to change the order of first and last names in the display name.
* This filter allows developers to rearrange the first and last name parts
* before they are combined to form the display name.
*
* @param array $name_parts An array containing the first name and last name, in that order by default.
* @param string $first_name The first name provided.
* @param string $last_name The last name provided.
* @return array The modified array of name parts.
*/
add_filter( 'ulgm_rest_api_display_name_order', function( $name_parts, $first_name, $last_name ) {
// By default, the order is [first_name, last_name].
// This example demonstrates reversing the order to 'Last Name First Name'.
// It also sanitizes the names to ensure they are strings and trims whitespace.
$sanitized_first_name = sanitize_text_field( (string) $first_name );
$sanitized_last_name = sanitize_text_field( (string) $last_name );
// Check if we want to swap the order. For demonstration, let's swap if the last name exists.
if ( ! empty( $sanitized_last_name ) ) {
return array( $sanitized_last_name, $sanitized_first_name );
}
// If no last name, just return the original (sanitized) parts.
return array( $sanitized_first_name, $sanitized_last_name );
}, 10, 3 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/helpers/rest-api-end-points.php:2027
// make sure email value is provided.
if ( ! empty( $email ) ) {
$args['user_email'] = $email;
}
if ( true === apply_filters( 'ulgm_rest_api_allow_display_name_update', false ) ) {
$display_name_order = apply_filters(
'ulgm_rest_api_display_name_order',
array( $first_name, $last_name ),
$first_name,
$last_name
);
$display_name = trim( $display_name_order[0] . ' ' . $display_name_order[1] );
$args['display_name'] = $display_name;