How to Change Comment Author in Your WordPress Website

WP-Admin has Comments section, which gives us ability to edit comments. But they let us edit only 4 fields: Name, Email, Url, Content.

But what about another data?  This bloggy section of WordPress is outmoded and needs improvements. For example if i change Name field there, it will just change the name field in wp_comments mysql table. But authorhip is defined by user_id and user email, not by name. So changing name is not serious change.

What about wp_update_comment( $commentarr ); function? It neither doesn’t help. Although we pass userid,email parameters via $commentarr  argument, it will not take any effect. Why? For knowing the reason we should check wp core code, namely wp-includes/comment.php, we will see such piece there:

$keys = array( 'comment_content', 'comment_author', 'comment_author_email', 
'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 
'comment_date_gmt', 'comment_parent' );
$data = wp_array_slice_assoc( $data, $keys );
$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );

It means that not all arguments are passed to wpdb update when we  use wp_update_comment function.

So the only way to change comment author correctly is working direcly with sql.

Here is simple code which successfully changes WP comment author:


$commentarr = array();
$newuser=get_user_by('id',$NEW_USER_ID_HERE);
$commentarr['comment_ID'] = $COMMENT_HERE;
$commentarr['user_id'] =$NEW_USER_ID_HERE;
$commentarr['comment_author'] = $newuser->display_name;
$commentarr['comment_author_email'] = $newuser->user_email;
global $wpdb;
$sqlc="update $wpdb->comments set user_id= ".$commentarr['user_id'].",
comment_author='".$commentarr['comment_author']."',
comment_author_email='".$commentarr['comment_author_email']."' 
where comment_ID= ".$commentarr['comment_ID'];
$wpdb->query($sqlc);

This is only main part of solution, to write full solution which should be integrated with wp-admin menu needs some additional simple codes.

1 comment on “How to Change Comment Author in Your WordPress Website

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.