Can i use username instead of user id in wordpress, ignoring wp_users table?

I am aware that wordpress maps the data of posts and comments in the database, with the user id.

For example, in wp_posts table, the column post_author corresponds to the user id who has written the post, and in wp_comments table, the column user_id corresponds to the user id who has commented on a post.

Read More

But what i am trying to achieve is to have a username value, instead of user id, to identify a user. Because i am developing a functionality such that the users will be authenticated outside of wordpress, so their details are not inserted in the wordpress database (wp_users table). I am using custom sessions to store the user’s session inside wordpress, storing the user’s username that i receive from a third party site, after successful authentication.

Having said the above, i am considering few options like:

  • Using hooks to alter the functionality of mapping/identifying a user by username, instead of user id.
  • Creating a new plugin that achieves this functionality
  • Create new tables inside wordpress and use them to store usernames along with the posts/comments.

Can anyone guide me in the right path?
Note that:

  • I just want to forget wp_users table. I don’t want to touch it. I will neither fetch anything from it, nor insert anything into it. So i just want to insert user login name into other related tables like wp_posts, and wp_comments, may be by adding new columns to them.
  • I also don’t want to change the core files of wordpress, as i am worried that i cannot update the wordpress later. But i believe that the database can be altered though.

Related posts

Leave a Reply

2 comments

  1. You don’t want to. Usernames are modifiable, they are not perfectly unique and come with a host of other related issues.

    Split up your application. If you want to have another authentication method then make it authenticate to a user using whatever method you like. Once you know which user it is you can get the user ID from the database and properly generate a WordPress login.

    Your modifications should avoid massive sweeping changes to the internals of a system that you aren’t very familiar with. That way lies madness. Instead, focus on building an add-on that does what you need and then hands control back to the primary system as quickly as possible. You avoid duplicating a lot of effort by doing it this way.

  2. I guess you are referring the user login name by saying username, right??
    If so then Yes you can achieve that because userlogin is unique like the userid and you can simply query your wp_users table to get the userid .
    If not then there’s no way to guarantee that you get the correct user’s info by username as mentioned by @mrdoombringer in previous answer