2 wordpress blogs with 1 users table and 1 login

Intro

Recently I was developing portal for nurses. I was trying to configure 2 WordPress installations. I needed them to be separate blogs. Multisite feature couldn't help me because secondary blog should contain huge database of agencies. And it would be just nightmare if both blogs will share same posts/comments tables in DB. Too many troubles I could get with MultiSite…

If you are in the same situation Solution is on this page for you!
It even works if your secondary blog located on subdomain.

How it works?
Here are 3 blogs:

  1. http://nokeywordinurl.com/
  2. http://nokeywordinurl.com/folder/
  3. http://blog.nokeywordinurl.com/

For testing - just sign up on one of them (doesn't matter on which one) and you'll get registered to all 3 blogs!

Requirements

  1. Both blog tables should share one database! (with different table prefixes)
  2. Access to phpMyAdmin for your DB (optional)
  3. WordPress CMS + root Cookie plugin + WP Orphanage Extended plugin

Solution works for WordPress 3.3.1 + root Cookie 1.6 + WP Orphanage Extended 1.0 (all of them are latest versions for today).

Solution Step-by-Step

1) Prepare wp-config for your main blog and setup it

  1. By default your wp-config.php has $table_prefix = 'wp_'; I didn't change it here.
  2. Generate secret keys&salts here - https://api.wordpress.org/secret-key/1.1/salt/
  3. Replace default keys&salts by generated code above.
  4. Install WP for your main blog (let's call it "blog A").

2) Prepare wp-config for your main blog and setup it

  1. For second blog ("blog B") change table prefix to another one. For example $table_prefix = 'blog_';
  2. Add to this config your secret keys&salts from wp-config of blog A.
  3. Now install your "blog B" with the same login/pass for admin.

3) Install root Cookie plugin on both blogs.

Select checkbox in it's settings to enable subdomain support if needed (WP-Admin->Settings->root Cookie).

Looks like "root Cookie" has a little bug at this time. I've spent couple of hours of testing that when accidentally found that it works only when you add this code in wp-config for both blogs.

1
define('COOKIEHASH', md5('http://www.YourDomain.com/'));

Don't forget to change "YourDomain.com" to domain name of your main blog.

Please let me know if you find the way to optimize cookies defining procedure (wp-config & root-Cookie plugin).

4) For both blogs install and activate WP Orphanage Extended plugin.
Visit setting page on both blogs (WP-Admin->Settings->WP Orphanage Extended).
Add all necessary prefixes there. In our example I've added "blog_" for blog A, "wp_" for blog B.

It will give access for users to their profiles on all installed blogs (doesn't matter on which one they registered).

Bug reported:
The problem appears when you register on blog A, login there and try to open /wp-admin/profile.php on blog B or C.
In this case you'll see "You do not have sufficient permissions to access this page."
User must visit wp-login.php on blogs B and C. After that they can access their profiles normally.

5) For secondary WP blog define users&usermeta tables in the wp-config.php
Use table prefix assigned in your main blog config (for blog A - "wp_").
Here is the code from my blog B:

1
2
3
$table_prefix = 'blog_';
define('CUSTOM_USER_TABLE', 'wp_users');
define('CUSTOM_USER_META_TABLE', 'wp_usermeta');

6) One step closer to the end…
Now you can't access to dashboard for second blog as admin. That's because WP Orphanage Extended makes it accessible only for users (normally set to Subscribers), but not for Administrator and other staff.

I've made the script that fixes that. Download zip-archive here.
This script should be placed into the root folder (public_html) of your main blog.
Don't forget to remove this script from your site when done.

Otherwise you need to give access to yourself manually.
Open phpMyAdmin and go to your MainBlogPrefix_usermeta (for our example it is 'wp_usermeta').
Click "Insert" and fill fields.
a)
user_id => 1
meta_key => SecondaryBlogPrefix_user_level
meta_value => 10

Adding user_level into wp_usermeta via phpMyAdmin

b)
user_id => 1
meta_key => SecondaryBlogPrefix_capabilities
meta_value => a:1:{s:13:"administrator";s:1:"1";}

Adding capabilities into wp_usermeta via phpMyAdmin

7) Done!
I'm not sure if this solution compatible with other versions of WP and plugins.
And I can't say troubles we can meet with that in future. But hope it will work like a charm.

What I'm waiting from you?
1) Please share your experience or ask for help here!
2) I hope someone can help with optimizing define cookies code.
3) Please link to this post when mentioning or reprinting this solution.
4) Subscribe to my blog and comments to this post. I'll get you updated for any changes.
I'm planning to use this solution on my main project. Just wish you to know that this question is very important for me too.

Special Thanks to meloniq for his plugin and real help with 90% of this solution.

VN:F [1.9.17_1161]
Rating: 7.4/10 (8 votes cast)
2 wordpress blogs with 1 users table and 1 login, 7.4 out of 10 based on 8 ratings
permalinkRead More CommentComments (21) CatWordpress

21 Responses to “2 wordpress blogs with 1 users table and 1 login”

  1. Chris Says:

    The cookie issue is an easy one, paste this into your wp-config.php file, but be sure to change the "my domain.com" to your top-level domain, not the www or any other subdomain:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    // Cookie Domain
    // This is a mydomain specific setting
    // The intent is to allow users a single signon experiance to all mydomain domains
    // However, this may interfear with the multi site setup if the users choose to use a different domain
    // So the idea is to set the cookie domain only if it is a mydomain.com domain or subdomain
    // Otherwise, just take the default behaviour
    if(substr_compare($_SERVER['HTTP_HOST'], 'mydomain.com',
     -strlen('mydomain.com'), strlen('mydomain.com')) == 0) {
    	// Define Cookie Domain for mydomain Sites
    	define('COOKIE_DOMAIN', 'mydomain.com');
    	// Set the Cookie Hash to something constant for mydomain Sites
    	// This should allow single signon if they have subscribed to the services
    	// This hash must then be used for the cookies themselves
    	define('COOKIE_HASH', md5('mydomain.com auth services'));
    	define('AUTH_COOKIE', 'wordpress_'.COOKIE_HASH);
    	define('SECURE_AUTH_COOKIE', 'wordpress_sec_'.COOKIE_HASH);
    	define('LOGGED_IN_COOKIE','wordpress_logged_in_'.COOKIE_HASH);
    	define('TEST_COOKIE', 'wordpress_test_cookie');
    }
    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  2. admin Says:

    Hi Chris!
    Thanks for that code.
    Bud do you really use it?
    I see you didn't specify cookie_path here.
    I mean define('COOKIEPATH', '/');

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  3. admin Says:

    Just found one bug with WP-Orphanage-Extended.

    The problem appears when you register on blog A, login there and try to open /wp-admin/profile.php on blog B or C.
    In this case you'll see "You do not have sufficient permissions to access this page."

    Visiting wp-login.php for each blog fixes that (it even doesn't ask for pass). After that all necessary prefixes added and all works like a charm.

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  4. Chris Says:

    Yes I do use it. The main site is a normal wordpress site and the second site is a multiuser site with a multi domain plugin. The setup works well and i have single sign on between the sites that use the same top level domain name. I can sign on to one site, and am automatically signed into the other sites. I can log out from one site and am automatically logged out of all the other sites. Single sign on does not work on the sites in the multisite setup that are on different top level domains. That is why i have the check to only set the cookie parameters only if they match my parent domain.

    I am using subdomains for everything, so the cookie path is defaulted to "/" which is why you do not see it in my config.

    I had problems with the Orphanage Extended plugin too, similar to what you describe. I stopped using the plugin and modified the core code to get around the issue. I modified the _init_caps() function in wp-includes/capabilities.php to get rid of the permissions issue. It works like a champ, however, you lose this fix on a wordpress core update. I am working on a way to extend the WP_User class itself to move the fix out of the core to get around the update issue.

    One other thing i did was break apart the wp-config.php file into separate files. I have a shared config file that is included in the wp-config file first thing. This shared config file has all the DB, salts, cookie, and some generic settings. The actual wp-config.php file only has the table_prefix setting. I did this so I could bring other sites up using the same database user and user metadata tables quickly.

    Send me an email if you want and I can send you my entire config files.

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  5. admin Says:

    Thanks Chris!
    Will contact you for sure!

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  6. GStock Says:

    The fix through using phpMyAdmin worked perfectly! I've been trying to fix the log in issues on Site2 and Site3 that had

    "You do not have sufficient permissions to access this page."

    Finally after hours of Google, I found your post on WordPress.org with the link to here.

    As for the cookies, I have successfully used the following and am able to log in/out of any of my 3 websites with no issues. Simply change the URLs to your own and paste in the config.php file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    $baseurl = 'http://www.YOURURLHERE.com';
    $cookiehash = md5($baseurl);
    define('COOKIEHASH', $cookiehash);
    define ('AUTH_COOKIE', 'wordpress_'.COOKIEHASH);
    define ('SECURE_AUTH_COOKIE', 'wordpress_sec_'.COOKIEHASH);
    define ('LOGGED_IN_COOKIE','wordpress_logged_in_'.COOKIEHASH);
    define ('TEST_COOKIE', 'wordpress_test_cookie');
     
    define('COOKIE_DOMAIN', '.YOURURLHERE.com');
    define('COOKIEPATH', '/');

    Also, I have made sure that AUTH_KEY, SECURE_AUTH_KEY, LOGGED_IN_KEY, NONCE_KEY, and all of their SALTS are the same in each config file for each website.

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  7. Fazio Says:

    Hello everyone, i add all these passages and for users login it works perfectly. But for me as manager when i go from may main domain into a subdomain, my capabilities are not as manager but only as a subscriber. I was thinking that the problem would be the fact that i have a WP login pass and ID, different from Mauindomian and Subdominas. es : maindomian wp login : admin = gestore , pass = 12345es : subdomain wp login : admin = gestore2, pass = 99988
    Do you think this would be the problem? ps. i can't login anymore on my WP subdomain (admin = gestore2, pass = 99988) as manager and manage my subdomain WP. Thank youFation

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  8. admin Says:

    Hi, Fazio!
    It's a good sign if new users can access to both blogs.

    What do you mean by "admin = gestore" ?
    Do you have regular configuration when main "admin" with user_id=1 ?
    You just changed login from admin to gestore? Or created new user gestore and assigned him administrator's role?

    Did you really made all as described in step 6 ?

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  9. Fazio Says:

    Hi Admin,

    It's a good sign if new users can access to both blogs.

    I think the same..:)

    What do you mean by "admin = gestore" ?

    this is an example of wordpress admin. I mean : for my first domain WP login i use different ID name and pass , from subdomain WP login name. I did all steps, include the 6 one. (I add the 2 string in maindomainPrefix_user_level and capabilities) . What do you mean here :

    Do you have regular configuration when main "admin" with user_id=1?

    Thank you.

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  10. Fazio Says:

    Hi Admin,
    in fact you was right, my "admin" user id was =5!!
    i changed it, = 1, but still don't work.
    when i try to get into dashboard : You do not have sufficient permissions to enter in this page.
    Here is what i did :
    install both 2 plugins in domain and subdomain,
    in sub-domain wp-config.php added:

    define('CUSTOM_USER_TABLE', 'maindomainprefix_users');
    define('CUSTOM_USER_META_TABLE', ' maindomainprefix _usermeta');

    in phpmyadmin maindomainPrefix_usermeta added :

    user_id meta_key meta_value1
    subdomainprefix_capabilities a:1:{s:13:"administrator";s:1:"1";}
    subdomainprefix_user_level 10 1 nickname "admin"

    also in sub-domina and main domain wp-config.php added:

    $baseurl = 'http://www.mydomain.com';
    $cookiehash = md5($baseurl);
    define('COOKIEHASH', $cookiehash);
    define('AUTH_SALT', 'AUTH_SALT'.COOKIEHASH);
    define('LOGGED_IN_SALT', 'LOGGED_IN_SALT'.COOKIEHASH);
    define('AUTH_COOKIE', 'AUTH_COOKIE'.COOKIEHASH);
    define('SECURE_AUTH_COOKIE', 'SECURE_AUTH_COOKIE'.COOKIEHASH);
    define('LOGGED_IN_COOKIE', 'LOGGED_IN_COOKIE'.COOKIEHASH);
    define('TEST_COOKIE', 'TEST_COOKIE'.COOKIEHASH);
    define('COOKIE_DOMAIN', '.mydomain.com');
    define('COOKIEPATH', '/');

    for users it works perfect, but not for my as Admin. Do you have any idea why this?
    thank you.

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  11. Radi Says:

    Hi,

    I read many guides on how to "2 wordpress blogs with 1 users table and 1 login" and non did it!

    Thanks a lot!

    VA:F [1.9.17_1161]
    Rating: 5.0/5 (2 votes cast)
  12. Radi Says:

    Hi again,

    I am having problem with the 1 login for my to sites. My set up is sitename.com/wordpress1/ and sitename.com/wordpress2/(in this one I have BuddyPress instalation)

    I have done everything you say and it works until you try to login... The shared user table work - I can register from wordpress1 site and login with the same username on wordpress2 without any problems, but I need to login for every site separately.

    I can see that after I login in the two websites they create 2 different cookies for each:

    name: wordpress_38f490d1cc4aa19b4d792ab9d875aaeb
    name: wordpress_logged_in_38f490d1cc4aa19b4d792ab9d875aaeb

    wordpress_8c8848dc05bf5a0a44af5ccc0204cd82
    wordpress_logged_in_8c8848dc05bf5a0a44af5ccc0204cd82

    host: sitename.com for both.

    My understanding is that the two should create only one and use it across.

    What do you thing the problem could be? Am I missing something in the settings? Could the problem be in my HOST provider? What about the .htaccess?

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  13. Chris MacKay Says:

    Thank you so much for taking the time to go through your process and post it for all to see!

    I spent a number of hours digging through Google search results trying to find just the right solution and each one was missing a different part of the process to making it work.

    The "fixdb.php" utility didn't seem to work for me but modifying the records manually via phpMyAdmin worked fantastically.

    Keep up the good work!

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  14. admin Says:

    Hi, Radi! Did you read this post?
    I mean this:

    ...But WordPress user registration differs from Buddypress and has less options. In this case registration on secondary blog disabled.

    You need register your test user on your /wordpress2/ (this one with Buddypress, right?).
    What versions of WP and BP you've installed?

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  15. Radi Says:

    I am not sure what I did different this time but after my ~15th try I have a "2 wordpress blogs with 1 users table and 1 login"

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  16. Luigi Says:

    You say : 

    Multisite feature couldn't help me because secondary blog should contain huge database of agencies. And it would be just nightmare if both blogs will share same posts/comments tables in DB.

    Are you sure ?

    WPMU create a separate wp1_posts, wp_postmeta etc  for each blog.

    Then ..  you make this hack to solve a non-problem :)

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  17. Harish Says:

    Hello just wanted to clarify one thing. You mentioned in the beginning that you did not use Multisite as you did not want same post/comments table to be used. But I think in Multisite different tables are created for each site's post/comments. Am I wrong?

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  18. admin Says:

    You guys are right. Will try WPMU for sure.

    But I have more than 15 plugins installed on my main WP-based site. Are all existent plugins are compatible with WPMU? What about BuddyPress and Cubepoints?

    VN:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  19. Tyler Says:

    Greetings,

    I seem to be having issues with this tutorial. I had it working last Friday but had to reinstall my subdomain due to an issue on my end and now I can't get it to work.

    I'll try and explain as clearly as possible:

    I have the main company site. Let's call it company.com. This company wants to have a "client portal" that I am building on a subdomain. We will call it "portal.company.com".

    I followed the instructions above for installing the root cookie plugin. I added the same keys&salts to both wp-config installations and then added the following code to the same file:

    <code>

    // Cookie Domain
    // This is a mydomain specific setting
    // The intent is to allow users a single signon experiance to all mydomain domains
    // However, this may interfear with the multi site setup if the users choose to use a different domain
    // So the idea is to set the cookie domain only if it is a mydomain.com domain or subdomain
    // Otherwise, just take the default behaviour
    if(substr_compare($_SERVER['HTTP_HOST'], 'mydomain.com',
     -strlen('mydomain.com'), strlen('mydomain.com')) == 0) {
    	// Define Cookie Domain for mydomain Sites
    	define('COOKIE_DOMAIN', 'mydomain.com');
    	// Set the Cookie Hash to something constant for mydomain Sites
    	// This should allow single signon if they have subscribed to the services
    	// This hash must then be used for the cookies themselves
    	define('COOKIE_HASH', md5('mydomain.com auth services'));
    	define('AUTH_COOKIE', 'wordpress_'.COOKIE_HASH);
    	define('SECURE_AUTH_COOKIE', 'wordpress_sec_'.COOKIE_HASH);
    	define('LOGGED_IN_COOKIE','wordpress_logged_in_'.COOKIE_HASH);
    	define('TEST_COOKIE', 'wordpress_test_cookie');
    }

    </code>

    Basically when I login to either site first (company.com or portal.company.com) it does not login to the other site. But if I try and login to the second site using the same user and password it will log the account out off of the first site. This works going both ways. I've tried various users and roles and it does it across the board. 

    All I'm trying to do is have a login form on company.com for portal.company.com so they can login to their account from the main site.

    Any idea what the issue may be?

    Thanks for the help.

    VA:F [1.9.17_1161]
    Rating: 4.0/5 (1 vote cast)
  20. Jake Rambo Says:

    Did you know that multisite would work just fine for this. Multisite does not share the same database tables for posts and comments. When you create a new site in mutli site it creates new tables in the database for every site you use.  It will add a new prefix to the tables for that specific site. Then you can use a plugin called Join My Multisite to automatically register users to both sites. 

    Everything will be separate but you will have the added ability to manage both sites from one dashboard. 

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  21. Sazzad Says:

    Hi

    This is very helpful information.

    I have used this method into one of my client's 6 websites. And it's working smoothly.

    Thank you so much for your nice article.

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

You must be logged in to post a comment.

CSS Template by RamblingSoul | Tomodachi theme by Theme Lab