[HOW TO] Change the user ID of comments in WordPress

Let us suppose that you have a blog based on WordPress and that you are also the administrator of that blog, of course. In such a case you should have at least an Admin account whose user ID value is generally set to 1. However, you may want to have also an Author account, different from the previous one, used to write posts and reply to else’s comments. This is particularly useful if you have a multi-author blog, but also in case of a single author blog.

So, let’s say that you have an Admin account as such:

Name:    admin
E-mail:  webmaster@yourblog.com
URL:     https://www.yourblog.com
ID:      1

and an Author account as such:

Name:    Your Name
E-mail:  yourname@yourpersonalsite.com
URL:     https://www.yourpersonalsite.com
ID:      2

Now, you probably are always logged in your blog as administrator, to manage it. If you wish to publish an article as Author you have two possibilities: logout as administrator and log in as author, or login as author on a different browser. It is good practice not to publish as administrator, and since you probably do not write ten articles per day, it is not a major problem to manage two accounts.

This is not necessarily true for comments. You may reply to many comments every day if your blog is popular, so that using two browsers continuously may be confusing whereas switching continuously from Admin to the Author account could be really annoying. Is there a way to reply from the Admin account publishing comments as author?

Yes, there is, if you have no problems to customize your theme. You can use a filter called preprocess_comment. Just add to functions.php in your current WordPress theme the following code snippet:

  function admin_to_author($comment_data){
    if ($comment_data['user_ID'] == 1) {
      $comment_data['user_ID'] = 2;
      $comment_data['comment_author'] = 'Your Name';
      $comment_data['comment_author_email'] = 'yourname@yourpersonalsite.com';
      $comment_data['comment_author_url'] = 'https://www.yourpersonalsite.com';
    return $comment_data;

If the Admin and the Author accounts are using different avatars, the avatar image will change too. Of course, while that filter is active, you cannot have any comment published with your Admin data, but this is not a problem, usually. If you are able to write WordPress plugins, anyway, you can develop a plugin based on that function to manage one or more substitutions. You can always reset to normal behavior by deactivating the plugin, in such a case.

Please, note that this works only for new comments. If you are editing a comment that you previously wrote as administrator, when you save it the author will not change to match the author account. Changing previous user IDs for comments requires to operate directly on the mySql database, which is recommended only for advanced users. Do not do it unless you perfectly know what you are doing, and always backup your database before!

I do not take any responsibility for damages to your database, but if you want to try, you might use a piece of code like this one:

$connection = mysql_connect("yourblog.com","admin","adminpassword") 
  or die("I cannot connect to mySql: " . mysql_error());
print ("Connected to mySql");

mysql_select_db("yourWPdatabase", $connection) 
	or die("Error to select database");

$query = "UPDATE wp_comments SET comment_author_url='https://www.yourpersonalsite.com' WHERE comment_author='Admin';" ;
$result = mysql_query($query,$connection);
if ($result==FALSE) die("error in updating author url");	
$query = "UPDATE wp_comments SET comment_author_email='yourname@yourpersonalsite.com' WHERE comment_author='Admin';" ;
$result = mysql_query($query,$connection);
if ($result==FALSE) die("error in updating author e-mail");

$query = "UPDATE wp_comments SET user_id='2' WHERE comment_author='Admin';" ;
$result = mysql_query($query,$connection);
if ($result==FALSE) die("error in updating user id");

$query = "UPDATE wp_comments SET comment_author='Your Name' WHERE comment_author='Admin';" ;
$result = mysql_query($query,$connection);
if ($result==FALSE) die("error in updating author name");


Please, note that the order of queries is important: changing the author’s name must always be the last query. You can also use myPhpAdmin, if you prefer to do it manually, of course.

Please use Facebook only for brief comments.
For longer comments you should use the text area at the bottom of the page.

Facebook Comments

Leave a Reply

In compliance with the appropriate provisions of the law I state that this site is no profit, has not a predefined recurrence and is not updated according to a deadline. It may therefore not be considered an editorial product under Italian law #62 of March 7th, 2001. In addition, this site makes use of the right of citation for academic and criticism provided in Article 10 of the Berne Convention on copyright.