It’s pretty useful to be able to share the same users database over two WordPress installations. This method is used by many important websites to keep sessions opened between different websites, domains or sub-domains. Here is a simple way to share users database:
Share users database to create a great user experience
Let’s say you have an existing WordPress installation, and you want to share the existing users database with a second website.
First of all make sure you are using the same WordPress version on the two websites and make sure your two websites are using the same database. To be able to have the two websites using the same database, you have to use different table prefix for each install.
To change the default WordPress table prefix, you have two choice. You can do it during the installation or editing the wp-config.php file where you must change $table_prefix value.
Once the second website has been installed, add these two lines in wp-config.php file, just before this line:
/* That's all, stop editing! Happy blogging. */
Add these two lines:
define('CUSTOM_USER_TABLE', 'wp_users');
define('CUSTOM_USERMETA_TABLE', 'wp_usermeta');
Make sure you replace “wp_” by the first website table prefix.
With this code you’re asking the second website to use the first website users table. Isn’t it great?
Now we are facing a small problem. Cookies… Yes, each website has its own cookies parameters, so your two websites have to use the same cookies parameters. So, still in wp-config.php of the second website, change the COOKIE_DOMAIN with the url of the first website:
define('COOKIE_DOMAIN', '.yoursite.com'); //replace with the 1st website url
define('COOKIEPATH', '/');
And now your’re done, now logged users will have their sessions opened accross your two websites.
Leave a Reply
You must be logged in to post a comment.