uknowit2
uknowit2

Reputation: 469

Wordpress Change Default Display Name Publicy As for all existing users

does anyone know of way I can change the existing display name publicly as for all users. I want to have firstname lastname as the default because this will reflect in the forum. I have read all the forums and have tried all the hacks, any suggestions would be appreciated. Thanks in advance

Upvotes: 11

Views: 22841

Answers (6)

Saad Tejda
Saad Tejda

Reputation: 11

use this code in function.php this code juste for change display_name and replace with first_name plus first letter for last_name in step the new user or update info for user .

//Change the publicly displayed name on comments
	add_filter('pre_user_display_name','default_display_name');
		function default_display_name($name) {
			 global $current_user;
     		 get_currentuserinfo();
			
		 $lastname=mb_substr($current_user->user_lastname, 0, 1, "UTF-8");
			$name =  ucfirst($current_user->user_firstname) .' '.ucfirst($lastname). '.';
			return $name;
		}

Upvotes: 0

rjb
rjb

Reputation: 9116

Here's an improved version of richplane's answer that works in newer versions of WordPress (3.8+):

/**
* Format WordPress User's "Display Name" to Full Name on Login
* ------------------------------------------------------------------------------
*/

add_action( 'wp_login', 'wpse_9326315_format_user_display_name_on_login' );

function wpse_9326315_format_user_display_name_on_login( $username ) {
    $user = get_user_by( 'login', $username );

    $first_name = get_user_meta( $user->ID, 'first_name', true );
    $last_name = get_user_meta( $user->ID, 'last_name', true );

    $full_name = trim( $first_name . ' ' . $last_name );

    if ( ! empty( $full_name ) && ( $user->data->display_name != $full_name ) ) {
        $userdata = array(
            'ID' => $user->ID,
            'display_name' => $full_name,
        );

        wp_update_user( $userdata );
    }
}

Upvotes: 14

Lou Terrailloune
Lou Terrailloune

Reputation: 721

A old question but my answer might be usefull for someone else.

Run this query on your database (adjust table names if you have another prefix):

UPDATE wp_users SET display_name = CONCAT((SELECT meta_value FROM wp_usermeta WHERE meta_key = 'first_name' AND user_id = ID), ' ', (SELECT meta_value FROM wp_usermeta WHERE meta_key = 'last_name' AND user_id = ID));

On WP 3.3.1 but should work on later versions.

Upvotes: 5

richplane
richplane

Reputation: 136

Problem with using the admin_head hook is that it doesn't work for users who don't use the admin system. Also, my attempts to implement the solution posted by Marty failed because it doesn't seem that the display_name can be updated by update_user_meta() - you have to use wp_update_user().

My proposal - put this in your functions.php file:

function force_pretty_displaynames($user_login, $user) {

    $outcome = trim(get_user_meta($user->ID, 'first_name', true) . " " . get_user_meta($user->ID, 'last_name', true));
    if (!empty($outcome) && ($user->data->display_name!=$outcome)) {
        wp_update_user( array ('ID' => $user->ID, 'display_name' => $outcome));    
    }
}
add_action('wp_login','force_pretty_displaynames',10,2); 

For me (using WP 3.4.1), that works OK, replacing the display name as soon as they log in.

Upvotes: 12

aubreypwd
aubreypwd

Reputation: 22930

<?php

//Sets the user's display name (always) to first name last name, when it's avail.
add_action ('admin_head','make_display_name_f_name_last_name');
function make_display_name_f_name_last_name(){

    $users = get_users(array('fields'=>'all'));

    foreach($users as $user){   
        $user = get_userdata($user->ID);    

        $display_name = $user->first_name . " " . $user->last_name;

        if($display_name!=' ') wp_update_user( array ('ID' => $user->ID, 'display_name' => $display_name) );
            else wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );

        if($user->display_name == '') 
            wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );
    }   
}

?>

Upvotes: 2

Marty
Marty

Reputation: 4657

Quick and dirty hack would be to edit the file 'wp-includes/user.php' edit the folowing

if ( empty($display_name) )
    $display_name = $user_login;
    $display_name = apply_filters('pre_user_display_name', $display_name);

Edit this line

$display_name = $user_login;

Change to:

$display_name = $first_name . ' ' . $last_name;

The above solution should work, assuming that the user.php file isn't changed in a WordPress update or alternativly you could add something like this to your functions.php

//force display-name of users to Firstname Lastname
add_action('admin_head','force_pretty_displaynames');
function force_pretty_displaynames() {
    $current_user = wp_get_current_user();
    if ($current_user->display_name != $current_user->first_name." ".$current_user->last_name){ 
       update_user_meta($current_user->ID, 'display_name', $current_user->first_name." ".$current_user->last_name);
    }

}

but agian a few checks on this above could be added to see if the user is logged in, is an admin, contributor etc..etc..

but should do what your looking for..

Source: http://wordpress.org/support/topic/change-default-display-name-1

Marty

Upvotes: -3

Related Questions