1

I want to have the same WordPress users in two different databases

For example, if a user registers on SiteA, then he can login to SiteB. And reverse.

Also i want create same cookie for both after login.

mywebsite.com/ (SiteA_DB)

mywebsite.com/blog/ (SiteB_DB)

Mr.Json
  • 139
  • 1
  • 2
  • 11
  • Based on the criteria you've listed in your question, you should be able to achieve this by setting up a WordPress Multisite installation using sub directories. There's a [detailed tutorial](https://wetopi.com/how-to-setup-wordpress-multisite-with-subdirectories/) available that walks through the set up process. Each site has its own tables in the database, but they all share a single `wp_user` table for the registered users. If you'd like, I can write up a more detailed answer later on for you. – Kirk Beard Oct 17 '18 at 08:24

1 Answers1

3

I've never done this before and maybe Wordpress has hooks to archive this, but I prefer using mysql for such a trick.

You could try ..

  1. .. using 'federated storage' ( https://stackoverflow.com/a/24532395/10362812 )
    This is my favorite, because you don't even have to share a database or even the mysql server
    The downside is, that it doesn't work with db cache and uses an additional connection.
  2. .. creating a 'view' ( https://stackoverflow.com/a/1890165/10362812 )
    This should be possible when using the database-name in the query itself and it would be the simplest solution if it works.
    Downside: The 2 tables have to share the same mysql-server and have to be assigned to the same user as far as I know.

    -- **Backup your database before trying!** --
    DROP TABLE `second_database`.`wp_users`;
    DROP TABLE `second_database`.`wp_usermeta`;
    CREATE VIEW `second_database`.`wp_users` AS SELECT * FROM `first_database`.`wp_users`;
    CREATE VIEW `second_database`.`wp_usermeta` AS SELECT * FROM `first_database`.`wp_usermeta`;
    

    This should work, according to: Creating view across different databases

  3. .. creating a 'shadow copy' ( https://stackoverflow.com/a/1890166/10362812 )
    Works with caching and is a standalone table
    Downsides as 2. solution + a bit of setup and I think it might be the worst option in performance

This were answers to this question: How do I create a table alias in MySQL
I merged them together for you and made them fit your use-case.

Please also notice, that solution 1 and 2 will replace your current user-tables auf "second_database" because you write directly into "first_database" when querying the fed. storage or the view. This can lead to problems with user-role plugins. You should take care of syncing the plugin-options too, if you should use one of them and in case it uses different tables or 'wp_options' values.

Let me know if this works, I have to do a similar task next week. While researching I found the linked answers.

EDIT: I was missing the point of "cookie-sharing" in my answer. Your example shows a blog on the same domain - you should be able to change the way wordpress sets its cookies to be domain-wide. What I did once for 2 different domains was, that I hooked into the backend (is_admin) and added a javascript which did a post-request to siteB, receiving a token which is stored but marked as 'invalid' on siteB. This token then was passed back to my plugin on siteA which checked if the user is logged_in and (in my case) have adminrights (current_user_can()) and if so, it was sending this token back to sideB which was marking this token as valid to login. (Make sure only sideA can tell sideB to make this token valid!) Once a user is seen with this token in a cookie on siteB, the user is logged-in automatically in the background. Also I made this bidirectional. I am sorry, that I can't share the code for you. I don't have access to it anymore.

Greetings, Eric!

  • Have you looked into using WordPress Multisite? It's designed to share the `wp_user` table amongst multiple WordPress sites. There's no need to manipulate MySQL directly, and you can rely on built-in WordPress functions to achieve the end result (in theory). – Kirk Beard Oct 17 '18 at 08:07
  • 1
    Hey Kirk! Multisites are not a suitable solution for me caused by several circumstances like for example the fact that both sites have to be running on the same machine (which is not needed when using 'federated storage' to share tables) or especially the fact, that I dont want to have to share the same filebase in case of an attack. Multisites are a good solution for people that are less experianced or have lower or other goals to achieve. But you may have made a good point for others finding this topic! – Eric Marten Oct 17 '18 at 10:40
  • No worries Eric, I don't know the full details of your requirements, but it's a very detailed answer for others. I thought I'd mention it, just in case. :) – Kirk Beard Oct 17 '18 at 10:58