WordPress does not allow for users to change their username, so neither does WP-Members. However, it does allow for the use of “display name” which is the name that is displayed on the site when showing users’ names. The default install of WP-Members does not make use of display name. In fact, by default, it adds the user’s username to this field so that it is not empty in the database, just in case you are using that information somewhere else on the site (such as in a forum).
But what if you want to make use of the display name field? How can you add that to the front-end user profile in WP-Members. This post will show you how to add it to the registration form (which by default includes the user update form), and then how to show it only in the user update form.
Adding the display name field
The display name field as handled by WordPress is stored as an entry in the wp_usermeta table. For those of you that may not be familiar with the WordPress default database structure and the WP-Members approach to user data, that is the same way all of the WP-Members extra fields are stored. So adding this to the user profile and registration process is nothing more than adding it as a field in the WP-Members field manager.
The main item of importance when creating the display name field in the field manager is to know that WP stores this as “display_name” in the wp_usermeta table. So when you create the field, to tie it into the WP native field, you need to give it the Option Name of “display_name” when you create the field.
Restricting the display name field to user update only
So adding the display name field is as simple as adding a new field in the field manager. What if you want this to be limited to the user profile update and not part of the initial registration?
That is where the wpmem_fields filter hook comes in. This filter allows you to easily change what fields are displayed in the form. The documentation for wpmem_fields displays how to use the filter to remove a field based on some criteria, which is what we will be doing in this example. In this case, we will remove the “display_name” field if the form being displayed is the registration form. That way the field is only included for user update.
So here’s the filter function to add to your functions.php file:
add_filter( 'wpmem_fields', 'my_remove_display_name_filter', 10, 2 ); function my_remove_display_name_filter( $fields, $tag ) { if ( 'register' == $tag ) { unset( $fields['display_name'] ); } return $fields; }
There is an extension of this tutorial that describes how to add the display name field to just the user profile update and use a dropdown selector similar to how the WP user profile screen is handled.