This is How to Display the First Letters of a User's Last Name in WordPress [Plugin]
As a WordPress developer, you may have encountered the need to display the first few letters of a user's last name alongside their first name. This is a common feature in user listing pages, author bios, and other places where you want to provide additional information about the user in a space-efficient manner.
In this blog post, we'll explore how you can create a simple WordPress plugin to achieve this functionality. By the end, you'll have a plugin that can be easily integrated into your WordPress website, enhancing the visual presentation of user information.
Understanding the Problem
Typically, when displaying a list of users or authors on your WordPress site, you might use the following code to output the user's name:
printf( '<a href="%s" title="%s">%s</a> <span class="post-count"><span class="hyphen">-</span> %s</span>',
get_author_posts_url( $user->ID ),
sprintf( esc_attr__( 'Read posts by %s', 'simple-user-listing' ), $user_info->first_name ),
$user_info->first_name,
sprintf( _nx( '1 post', '%s posts', $num_posts, 'number of posts', 'simple-user-listing' ), $num_posts )
);
This code will display the user's first name and the number of posts they have written. However, it does not include the user's last name or any indication of their last name.
To display the first few letters of the user's last name, you can use the substr()
function to extract the first 3 characters of the last name and append it to the output. Here's an example:
$get_title = get_the_author_meta('user_lastname');
$ltr_group = substr($get_title, 0, 3);
printf( '<a href="%s" title="%s">%s %s</a> <span class="post-count"><span class="hyphen">-</span> %s</span>',
get_author_posts_url( $user->ID ),
sprintf( esc_attr__( 'Read posts by %s', 'simple-user-listing' ), $user_info->first_name ),
$user_info->first_name,
$ltr_group,
sprintf( _nx( '1 post', '%s posts', $num_posts, 'number of posts', 'simple-user-listing' ), $num_posts )
);
This code first retrieves the user's last name using the get_the_author_meta()
function, stores it in the $get_title
variable, and then uses the substr()
function to extract the first 3 characters and store them in the $ltr_group
variable. Finally, the printf()
function is used to output the user's first name and the first 3 letters of their last name.
Creating a WordPress Plugin
To make this functionality more reusable and easier to integrate into your WordPress site, let's create a simple plugin that can be installed and activated.
- Create the Plugin File: Create a new file in your WordPress plugin directory (usually located at
wp-content/plugins/
) with the following contents:
<?php
/**
* Plugin Name: Display First Letters of Last Name
* Plugin URI: https://flowpoint.ai
* Description: Displays the first 3 letters of a user's last name alongside their first name.
* Version: 1.0
* Author: Flowpoint.ai
* Author URI: https://flowpoint.ai
*/
function display_first_letters_of_lastname( $content ) {
global $user_info;
if ( $user_info->last_name ) {
$ltr_group = substr( $user_info->last_name, 0, 3 );
$content .= " $ltr_group";
}
return $content;
}
add_filter( 'the_author_display_name', 'display_first_letters_of_lastname' );
- Activate the Plugin: Log in to your WordPress admin dashboard, go to the "Plugins" section, and activate the "Display First Letters of Last Name" plugin.
Now, whenever you display a list of authors or users on your WordPress site, the first 3 letters of their last name will be appended to their first name.
Customizing the Plugin
If you want to customize the plugin further, you can make the following changes:
- Change the Number of Letters Displayed: If you want to display more or fewer than 3 letters of the last name, you can modify the
substr()
function call in the display_first_letters_of_lastname()
function:
$ltr_group = substr( $user_info->last_name, 0, 2 ); // Display the first 2 letters
$ltr_group = substr( $user_info->last_name, 0, 4 ); // Display the first 4 letters
- Apply Additional Styling: You can add CSS classes or styles to the output to match the design of your website. For example, you can wrap the last name letters in a
<span>
tag with a specific class:
$ltr_group = "<span class='last-name-letters'>$ltr_group</span>";
$content .= " $ltr_group";
- Integrate with Other Plugins: If you're using a plugin that displays author information, such as the "Simple User Listing" plugin, you can integrate the "Display First Letters of Last Name" plugin by modifying the code snippet provided earlier:
printf( '<a href="%s" title="%s">%s %s</a> <span class="post-count"><span class="hyphen">-</span> %s</span>',
get_author_posts_url( $user->ID ),
sprintf( esc_attr__( 'Read posts by %s', 'simple-user-listing' ), $user_info->first_name ),
$user_info->first_name,
$ltr_group,
sprintf( _nx( '1 post', '%s posts', $num_posts, 'number of posts', 'simple-user-listing' ), $num_posts )
);
By creating this simple plugin, you can easily add the functionality of displaying the first few letters of a user's last name to your WordPress site, improving the visual presentation and organization of user information.
Remember, Flowpoint.ai can help you identify all the technical errors that are impacting conversion rates on your website and directly generate recommendations to fix them, including issues related to user interface and user experience
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.