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:

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

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:

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

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

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

%d bloggers like this: