This post is a quick overview of the post [HOW TO] Change the user ID of comments in WordPress, from L’Independente.
How to Change Comment Author in WordPress
If you’re like me, you often write your blog posts while logged in under your admin account. This is pretty common among most WordPress users since they’re usually doing some other things when they write a post.
Best Security Practice for Writing Posts
The best practice for securely writing blog posts is to actually sign in using an Author role and write your post that way. The reason this is good to do is because it lets you keep a secret administrative username and, if your login credentials are ever stolen, it decreases the risk that the thief will gain admin access. Honestly, I don’t really do that and neither do many WordPress users. It’s a nuisance to have to log in and out switching between users. The only time I sign in under an Author account and absolutely suggest doing that is if you are going to sign in on a public network and your site does not have SSL encryption.
Create an Author Account
If you’re reading this post, you probably already have a separate Author account in place. Just to make note of it, create a new user and assign that user to the Author role. This will be the author of all the posts that you write. Make sure to give Author the name you want to appear in the post’s meta, in the author box (if you have one), and in Google. Also, make sure that your admin display name is different from your Author display name so you don’t confuse the two.
When you create a new post while signed into your admin account, you will need to make sure that you set the Post Author to the proper user before you publish. This can be done within the Add New Post page. If you do not see the Author section, check to make sure it is enabled in the Screen Options (top-right corner). If you preview your posts before publishing them like you should, you’ll see that the post displays the meta information of the proper Author and not the Admin you’re signed in under.
If you want to switch back and forth between users, there is a nice plugin called User Switching that lets you do so without having to log in and out.
Now that you’ve taken care of publishing your posts, we need to cover commenting. Creating and assigning new posts to a separate Author account is pretty simple once you make it a routine. However, comments cannot assigned to another user in the same way posts can. When you comment while signed in under an admin account, the comment is attributed to that admin. This may be no big deal when you first think about it but it actually is. One of the main reasons to publishing under a different account is for security; you don’t want to reveal any of your admin info. The problem that occurs with commenting under your admin account is that WordPress assigns a class of your username to your comments. If you inspect the list item (li) element of one of your replies, you will notice that the li contains a class with the username of the account under which you posted that comment. The HTML will look something like:
This makes your username clear as day. If you’re commenting under your admin account, a potential hacker now knows half your administrative login.
Ideally, you’d like to still be able to comment under your admin account without having to switch back and forth between accounts, right? The code below will let you do that. Just paste it into your functions.php or custom plugin.
What this snippet does is gets the ID of the comment author and, if it is the ID you set (i.e. your admin ID), then is does the following:
- Changes the comment author ID to another ID (i.e. your author ID)
- Sets the name you want to display when you comment
- Sets the email of the Gravatar image you want to display
- Sets the website URL you want to link to your name
- Returns this information to the function, which then process the change when a new comment is made
NOTE: This code does not reassign existing comments. It only works for new comments. To reassign existing comments, check out the original post for some additional information.
You can now publish both posts and comments under a special Author account without having to worry about leaving traces of your admin account.