How to get commenter ID in WordPress

Home / WordPress / How to get commenter ID in WordPress

If you want to output some data (such as biographical info or personal website) for the person who commented on WordPress post or page, you should be able to get the user ID so you could use it for the_author_meta() function. One of the ways to do it is by using the following code – the first line returns commenter ID, while other lines returns some specific data:

But at this point, if you’re asking yourself  “What is the ID for anonymous commenter”, your question is right on the spot. I believe that this code can be useful only if you allow only registered users to comment on your posts, which was exactly the case I had which that led me to research this method.

Also, even if you allow anyone to post comments on your blog, the $user_id should return 0 for anonymous commenters (whoever is not registered as user, those are).