2 Wordpress sites with 2 databases but sharing the same users

I have 2 Wordpress installation: siteA and siteB. Each installation have their own wordpress database: databaseA for siteA and databaseB for siteB.

What I want is users on siteA can connect on siteB. I can't use CUSTOM_USER_TABLE beacause is 2 separate databases. However the database are hosted on the same server. So, how can I use the users table of siteA for siteB with my configuration?

Topic sharing users database Wordpress

Category Web


This ACTUALLY WORKS for me, but hey, warning and disclaimer first: Do this at your own risk.

It was pretty simple. Just "share" relevant tables between databases using views. I'm on MySQL / MariaDB so you should modify these statements if you're using a different DBMS.

USE siteB;
ALTER TABLE wp_users RENAME TO wp_users_bak;
ALTER TABLE wp_usermeta RENAME TO wp_usermeta_bak;
CREATE VIEW wp_users AS SELECT * FROM siteA.wp_users;
CREATE VIEW wp_usermeta AS SELECT * FROM siteA.wp_usermeta;

Keep in mind you have to use the same set of user-related plugins or you'll likely run into problems soon.


Yes it's possible, despite the answers above. If you want to keep the websites completely separated, you can with WP Remote Users Sync (disclaimer: I am the author of the plugin)

Edit - to elaborate on the context and method used as asked in comments:

The way it works is that each time there is a change in user data (login, logout, create, update, delete, roles, password, metadata, using WordPress hooks and by redefining pluggable functions), a call is made to all registered remote sites (using a <script> tag for login and logout, so that cookies can be shared, and wp_remote_post in all other cases) using a specific endpoint (my-website.com/wprus/[action_name]). Then, on the remote site, the calls are listened and caught, the data is parsed, and the users are synced.

What sets this plugin apart from what already exists is the security layers in place: Communications are encrypted with single use initialization vectors, hmac-signed, token-validated and IP-validated to make the process as secure as possible.

What pushed me to implement it is that either the solution did not exist, or even commercial solutions out there were not suitable (the most popular and biggest offender being Share Logins Across Multiple Sites which is up to 250 USD per year and yet vulnerable in many ways, worst of which being iframe injection, but more info on that is worth its own dedicated post).

Syncing websites using separate domains, databases, and servers (and more so with secure token exchange) also means that there is an unavoidable marginal performance overhead for each user-related operation for each connected remote site (remote calls).


Merge the database content via WPRSS Import/export in a Multisite installation, one install, one maintenance and a default option to share users about the two sites. So you need no 3th party plugins and use different sites with different domains in one installation.

Benefits

  • Maintenance, one-time upgrade
  • Shared Plugins, Themes
  • Shared Users
  • Quick Access To Many Blogs
  • Advanced settings
  • Standard, no 3th party plugins, changes necessary

I was also thinking about doing this. So instead of having 2 different databases, you'd have just the one. And I know the table prefix can be different. So like wp_12 & wp_21 But I guess you need both salts to be the same on both sites?

I think with their issue is that it has 2 seperate DB's. So it would be best for them to make a new DB, and export the other 2 and import them into the new database. But that's providing that the table prefix is different for each site.


It's not possible in Wordpress to have shared users between 2 separates installations on 2 separated databases.

It could be possible just by hardcoding:

  1. hardcode WP_User class connect to other database and retrieve users from there
  2. hardcode get_metadata() function, that for wp_usermeta table connect to other database and retrieve users from there

but mentioned solution is not recommended of course, because it's not updateproof.


you can do it by making changes in wp_config file of site B On site A, let’s say it uses the ‘wp_1_’ table_prefix. For site B, you need to change the prefix from $table_prefix = ‘wp_1_’;

to:

$table_prefix = ‘wp_2_’;

In Site B's config file add these codes:

define(‘CUSTOM_USER_TABLE’, ‘wp_1_users’);

define(‘CUSTOM_USER_META_TABLE’, ‘wp_1_usermeta’);

Also replace the salt keys in wp-config file of site A to site B

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.