• Resolved Grindabit

    (@grindabit)


    I searched everywhere for a fix to this problem and saw that a few others had issues finding the answer as well. I thought I would post what I came up with as a solution. It was a simple fix.

    Here is what this modification does:
    Do you want to make sure nobody can change their display name by manipulating their firstname and the “Display username publicy as:” feature?

    This is useful if you run a chat application on your site that uses the wordpress/buddypress user system to sign your chat users on.

    For example, by default a user can make their username “Blueuser” – If Blueuser wants to impersonate an admin named “RedAdmin” all that blueuser needs to do is change his first name in his profile to “RedAdmin” and then select “Display Username Publicy as: RedAdmin” and walla! Blueuser now appears in chat as RedAdmin

    Here is how to fix this problem.

    Locate the following file in your wordpress installation:
    /wp-admin/user-edit.php

    First, make a back up copy of this file adding a ~ before the name ( ~user-edit.php) just in case you mess things up.

    Open the file user-edit.php

    Find and delete the following lines of code:

    <tr>
    	<th><label for="display_name"><?php _e('Display name publicly as') ?></label></th>
    	<td>
    		<select name="display_name" id="display_name">
    		<?php
    			$public_display = array();
    			$public_display['display_nickname']  = $profileuser->nickname;
    			$public_display['display_username']  = $profileuser->user_login;
    
    			if ( !empty($profileuser->first_name) )
    				$public_display['display_firstname'] = $profileuser->first_name;
    
    			if ( !empty($profileuser->last_name) )
    				$public_display['display_lastname'] = $profileuser->last_name;
    
    			if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) {
    				$public_display['display_firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name;
    				$public_display['display_lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name;
    			}
    
    			if ( !in_array( $profileuser->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere
    				$public_display = array( 'display_displayname' => $profileuser->display_name ) + $public_display;
    
    			$public_display = array_map( 'trim', $public_display );
    			$public_display = array_unique( $public_display );
    
    			foreach ( $public_display as $id => $item ) {
    		?>
    			<option <?php selected( $profileuser->display_name, $item ); ?>><?php echo $item; ?></option>
    		<?php
    			}
    		?>
    		</select>
    	</td>
    </tr>

    You have now removed the ability for the user to select their display name. By default, usernames will now only display. First name will not be an option anymore.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Grindabit,

    I’ll give you credit for finding a solution to your problem. However, I highly recommend you DO NOT DO THIS.

    Modifying the WordPress core files (anything in the wp-admin or wp-includes folders) is a really bad idea. When an update is made to WordPress, your changes will be lost. Don’t forget, WordPress has automatic updates for minor releases. Your code will be overwritten with no warning.

    The proper way to do this would be by adding the following code to your functions.php in your theme, or creating a plugin with the same code (my preference):

    add_action('show_user_profile', 'remove_display_name');
    add_action('edit_user_profile', 'remove_display_name');
    
    function remove_display_name($user) { ?>
    	<script>
    		jQuery(document).ready(function() {
    			jQuery('#display_name').parent().parent().hide();
    		});
    	</script>
    <?php }
    Thread Starter Grindabit

    (@grindabit)

    Hey Shawn,

    Thanks for the reply, So to create a plugin file I would just save that code as a php file, upload it to the plugins directory then activate it?

    or…if I was to just add it to the functions file…where would I add that?

    In the functions file, just put it at the bottom.

    To make a plugin, it needs to have a header, like this:

    <?php
    /**
     * Plugin Name: Name Of The Plugin
     * Plugin URI: https://URI_Of_Page_Describing_Plugin_and_Updates
     * Description: A brief description of the Plugin.
     * Version: The Plugin's Version Number, e.g.: 1.0
     * Author: Name Of The Plugin Author
     * Author URI: https://URI_Of_The_Plugin_Author
     * License: A "Slug" license name e.g. GPL2
     */

    followed by your code. Save that, for example, as remove_display_name.php and put it in the plugins folder.

    Note: There are better practices that this for creating larger plugins, but it will work for what you need.

    Don’t forget to activate the plugin in the Dashboard.

    Thread Starter Grindabit

    (@grindabit)

    awesome! thanks for the help! It worked!

    Thread Starter Grindabit

    (@grindabit)

    Here is the plugin I installed in case someone else needs it.

    <?php
    /**
    * Plugin Name: Remove_username
    * Plugin URI: https://URI_Of_Page_Describing_Plugin_and_Updates
    * Description: Removes ability for user to change public display name.
    * Version: The Plugin’s Version Number, e.g.: 1.0
    * Author: Shawn Hooper
    * Author URI: https://URI_Of_The_Plugin_Author
    * License: GPL2
    */
    add_action(‘show_user_profile’, ‘remove_display_name’);
    add_action(‘edit_user_profile’, ‘remove_display_name’);

    function remove_display_name($user) { ?>
    <script>
    jQuery(document).ready(function() {
    jQuery(‘#display_name’).parent().parent().hide();
    });
    </script>
    <?php }

    Something like this, removing the option for users to choose display names that are different from their username, is sorely needed!
    But instead of using a new plugin,
    Can’t permissions be changed for the subscriber role?
    is there a permission for changing display name??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Remove Option in user profile to change public displayname’ is closed to new replies.