WordPress user_login VS user_nicename
In WordPress while we create any user from dashboard or any user register admin or user provides login id or username or email and password plus more information based on how the registration process is designed in your site. As soon as new user is registered a new entry/row is made/created in wordpress user table.
If you check in wordpress user table which is wp_users or dbprefix_users has multiple columns/fields where two specific cols/fields name are ‘user_login’ and ‘user_nicename’. ‘user_login’ field is used for username or login. But ‘user_nicename’ is not the same but it’s but different than ‘user_login’ field. ‘user_nicename’ field is actually slug value of ‘user_login’.
If you are known about slug concept of wordpress or seo friend url then you know when we create any article wordpress creates an unique slug for that article from the title. That slug field is used for creating permalink. We can manually change that slug or permalink. Same way, any user is created from the user_login field a new slug is created for the user’s permalink which is stored in col/field named ‘user_nicename’. We mostly don’t notice this because this is not shown in admin but can be used in different purposes. Like in buddypress we see user’s profile url where the nicename field is used.
I am giving an example of how the user_login and user_nicename field is created from different types of username used while registering or creating user.
How we can get the nice name of a user
Let’s find the current logged in user id and it’s nice name. The below code is an example of this task.
<code class="language-php"> $user_id = absint(get_current_user_id());//find current logged in user's id $user = get_user_by('ID', $user_id); //get user by 'ID' field if($user !== false){ //if user is found then get the user nicename $user_nicename = $user->user_nicename; } </code>
Other Use of user_nicename
This user_nicename field also used in core wordpress any user’s post archive or blog archive. Function ‘get_author_posts_url’ used to get any user’s archive url , the 2nd param of this function is user_nicename. In below code we linked in the user’s nicename field with the frontend archive page.
<code class="language-php"> /** * Retrieve the URL to the author page for the user with the ID provided. * * @since 2.1.0 * * @global WP_Rewrite $wp_rewrite WordPress rewrite component. * * @param int $author_id Author ID. * @param string $author_nicename Optional. The author's nicename (slug). Default empty. * @return string The URL to the author's page. */ function get_author_posts_url( $author_id, $author_nicename = '' ) { global $wp_rewrite; $auth_ID = (int) $author_id; $link = $wp_rewrite->get_author_permastruct(); if ( empty( $link ) ) { $file = home_url( '/' ); $link = $file . '?author=' . $auth_ID; } else { if ( '' === $author_nicename ) { $user = get_userdata( $author_id ); if ( ! empty( $user->user_nicename ) ) { $author_nicename = $user->user_nicename; } } $link = str_replace( '%author%', $author_nicename, $link ); $link = home_url( user_trailingslashit( $link ) ); } /** * Filters the URL to the author's page. * * @since 2.1.0 * * @param string $link The URL to the author's page. * @param int $author_id The author's ID. * @param string $author_nicename The author's nice name. */ $link = apply_filters( 'author_link', $link, $author_id, $author_nicename ); return $link; } </code>
Displaying user_nicename in WordPress User Listing
By default wordpress doesn’t show the user_nicename field in dashboard user listing but we can display this using simple few lines of code that is presented below. You can copy paste this code in your theme’s functions.php file.
<code class="language-php"> /** * Add new col 'Nicename' for dashboard user listing * * @param $column * * @return mixed */ function codeboxr_modify_user_table1( $column ) { $column['user_nicename'] = __('Nicename', 'codeboxr'); return $column; } add_filter( 'manage_users_columns', 'codeboxr_modify_user_table1' ); /** * Display 'user_nicename' field value for the new registered col * * @param $value * @param $column_name * @param $user_id * * @return mixed|string */ function codeboxr_show_user_id_column_content($value, $column_name, $user_id) { $user = get_user_by('ID', $user_id); //get user by 'ID' field if($user !== false){ //if user is found then get the user nicename $user_nicename = $user->user_nicename; //return $user_nicename; $user_url = get_author_posts_url($user_id, $user_nicename); return '<a href="'.esc_url($user_url).'" target="_blank" rel="noopener">'.$user_nicename.'</a>'; } return $value; } add_filter('manage_users_custom_column', 'codeboxr_show_user_id_column_content', 10, 3); </code>
Any question? Ask in comment or via our contact page.