WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 5 years ago

#7494 closed defect (bug) (fixed)

registered members displayed as "anonymous" in comments if display_name empty (has fix)

Reported by: _ck_ Owned by:
Milestone: 2.7 Priority: normal
Severity: normal Version: 2.6
Component: General Keywords: has-patch
Focuses: Cc:

Description

There is a bug since WP 1.5 where if the display_name is empty when a comment is posted, WordPress does not fall back to the user_login but instead saves the comment_author as blank. This causes the comment template to display registered members as "anonymous".

While typically in WordPress the stored display_name would not be blank, other software that shares the user table such as bbPress does not save a display_name by default during it's registration process. I am unsure if this is an attempt to save database space, but it makes sense since if the display_name == the user_login, why duplicate the data (on a million users, this would add up significantly).

The bug is being addressed on the bbPress side:
http://trac.bbpress.org/ticket/922

however, why not take the opportunity to fix the bug on the WordPress side as well:

it is caused by line 43 in wp-comment-post.php in WP 2.6.1

if ( $user->ID ) {
	$comment_author       = $wpdb->escape($user->display_name);

Should be changed to something like:

if ( $user->ID ) {
if (empty($user->display_name)) 	{$user->display_name=$user->user_login;}
$comment_author       = $wpdb->escape($user->display_name);

The above fix of course does not fix existing entries of course.
To do that, the function get_comment_author in comment-template.php must be augmented with similar code.

function get_comment_author() {
	global $comment;
if (empty($comment->comment_author) && !empty($comment->user_id)) {$user=get_userdata($comment->user_id); $author=$user->user_login;}

	if ( empty($comment->comment_author) ) {
                if (!empty($comment->user_id) ) {
                     $author=$user->user_login;
                } else {$author = __('Anonymous');}
	} else {
		$author = $comment->comment_author;
        }
	return apply_filters('get_comment_author', $author);
}

Attachments (2)

wp-comments-post.diff (480 bytes) - added by mrmist 6 years ago.
wp-comments-post patch against 8772
comment-template.diff (604 bytes) - added by mrmist 6 years ago.
wp-includes\comment-template patch against 8772

Download all attachments as: .zip

Change History (8)

comment:1 _ck_6 years ago

There is a serious typo in my above replacement function, sorry:

function get_comment_author() {
	global $comment;

	if ( empty($comment->comment_author) ) {
              if (!empty($comment->user_id)){ 
                     $user=get_userdata($comment->user_id);       
                     $author=$user->user_login;
                } else {$author = __('Anonymous');}
	} else {
		$author = $comment->comment_author;
        }
	return apply_filters('get_comment_author', $author);
}

comment:2 _ck_6 years ago

  • Milestone changed from 2.7 to 2.6.1

comment:3 santosj6 years ago

It doesn't matter if it is there or not, the space is still being used. Whether it is padded because it is empty or filled with data. The space used will still be the same.

comment:4 westi6 years ago

  • Milestone changed from 2.6.1 to 2.6.2

2.6.1 has been released, moving to 2.6.2 milestone

mrmist6 years ago

wp-comments-post patch against 8772

comment:5 mrmist6 years ago

  • Keywords has-patch added

Added patch files for the suggested alterations, though I'm not convinced that there is the right place to be doing this - would probably be better to fix plugins that are not filling in display names, or alter the methods they are using so that they fill in all the info when setting up / importing new users...

mrmist6 years ago

wp-includes\comment-template patch against 8772

comment:6 markjaquith5 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [9781]) Handle blank display_name for commenters. props mrmist. fixes #7494

Note: See TracTickets for help on using tickets.