The ‘pre_user_last_name’ filter allows you to modify a user’s last name before the user is created or updated in WordPress.
Usage
$filtered_last_name = apply_filters( 'pre_user_last_name', $last_name );
Parameters
$last_name
(string) – The user’s last name.
Examples
Adding a Prefix to Last Name
function add_prefix_to_last_name( $last_name ) { return 'Dr. ' . $last_name; } add_filter( 'pre_user_last_name', 'add_prefix_to_last_name' );
This code adds the prefix “Dr. ” to the user’s last name before it is saved.
Capitalizing Last Name
function capitalize_last_name( $last_name ) { return strtoupper( $last_name ); } add_filter( 'pre_user_last_name', 'capitalize_last_name' );
This code capitalizes the entire last name before it is saved.
Removing Whitespace
function remove_whitespace_from_last_name( $last_name ) { return preg_replace( '/\s+/\', '', $last_name ); } add_filter( 'pre_user_last_name', 'remove_whitespace_from_last_name' );
This code removes all whitespace characters from the user’s last name before it is saved.
Replacing Hyphens with Spaces
function replace_hyphen_with_space( $last_name ) { return str_replace( '-', ' ', $last_name ); } add_filter( 'pre_user_last_name', 'replace_hyphen_with_space' );
This code replaces any hyphens in the user’s last name with spaces before it is saved.
Abbreviating Last Name
function abbreviate_last_name( $last_name ) { return substr( $last_name, 0, 1 ) . '.'; } add_filter( 'pre_user_last_name', 'abbreviate_last_name' );
This code abbreviates the user’s last name to the first letter followed by a period before it is saved.