WordPress.org

Ready to get started?Download WordPress

Forums

multiple sites, same users - how? (17 posts)

  1. jeremy
    Member
    Posted 8 years ago #

    I realize this has been covered as a part of larger discussions on entire database sharing, but I am a complete idiot when it comes to programming and I need someone to clearly walk me through some steps.

    I have a WordPress site with about 300 users. I am about to launch another 3-6 WordPress sites, with unique content, but I want them all to share the same user database. This way people won't have to create new accounts.

    I realize I need to establish these new sites in the same database as the existing site, and make it so they all reference the same users table. Can someone please walk me through this process? I would be so grateful.

    Thanks,
    Jeremy

  2. Mark (podz)
    Support Maven
    Posted 8 years ago #

    I haven't done this, but I think:

    - On the major blog, use phpmyadmin to export JUST the wp_users table.
    - On each subsequent blog, DROP the wp_users table then IMPORT the wp_users table that you had exported.
    I can't see why that would not work, but I would carefully test it first.

    Exporting is covered here with the backup guide - as is restoring - but in the 4th image (with the red numbers) just select the wp_users table.

    http://www.tamba2.org.uk/wordpress/backup/

  3. jeremy
    Member
    Posted 8 years ago #

    But this will just copy users over, right? I want everyone who creates an account on any of the sites to have access to all of them, whether they created an account a month ago or two weeks from now.

  4. Mark (podz)
    Support Maven
    Posted 8 years ago #

    It would copy the whole users table. Passwords, ID Numbers, the lot.

  5. dss
    Member
    Posted 8 years ago #

    Or, if you really want them to be identical and to include new users, then you could try creating the appearance of different sites by styling the new areas differently, and using fake folders, sub domains, etc. to provide the feel of separation. But that might not be easier.

  6. Joshua Sigar
    Member
    Posted 8 years ago #

    In wp-settings.php line 73-76, there is the following.
    if ( defined('CUSTOM_USER_TABLE') )
    $wpdb->users = CUSTOM_USER_TABLE;
    if ( defined('CUSTOM_USER_META_TABLE') )
    $wpdb->usermeta = CUSTOM_USER_META_TABLE;

    So, you could probably have the following in index.php in the new wordpress' root directory. (see note)


    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', true);
    define('CUSTOM_USER_TABLE', 'wp_users');
    define('CUSTOM_USER_META_TABLE', 'wp_usermeta')
    require('./wp-blog-header.php');
    ?>

    Note: In this example, first installation was installed with table prefix of wp_ and the second installation's prefix is wp2_ or any arbitrary prefix. The code above goes to the second installation. This way, no tables duplication necessary.

    I haven't tried this myself, but this is the concept.

  7. jeremy
    Member
    Posted 8 years ago #

    OK, I got it working. Without the above help I would have been lost, but I did need to make a few tweaks and additions. Here's the formula in case anyone else wants to do something similar.

    Edit the 'wp-settings.php' file for every site. Around line 73 you'll see:

    if ( defined('CUSTOM_USER_TABLE') )
    $wpdb->users = CUSTOM_USER_TABLE;

    Insert a couple of lines above that, and type the following:

    define('CUSTOM_USER_TABLE', 'wp_users');

    This assures all blogs access the same user database. I tried adding that to the index.php as alphaoide suggested, but it didn't work. In the settings file however, it's golden.

    Then you have to edit the 'wp-config.php' file for every site. On line 5 you'll see:

    define('DB_NAME', 'your_database_name');

    All of your blogs should have an identical database name. Point them to whatever is your master blog with the user database you want to use. Then, on line 17 you'll see:

    $table_prefix = 'wp_';

    This is important. Every blog needs to have a unique prefix. I left this default setting for the master blog, but for each additional blog I changed this to something new (ie. 'wp2_', wp_blog5', etc.).

    You're almost set. Now for each blog you must launch '/wp-admin/install.php' and let it create all the new tables in your database. Once that's complete, you should have separate working blogs sharing the same user database.

    Now, up til now, this works only if you're creating fresh new WordPress blogs with no content yet. If you want to merge the user databases of two existing sites with existing content, it gets a little tricky. After following the above, you have to copy the tables from your old database(s) into the master 'DB_NAME' assigned above. I did this using PHPMyAdmin, which is an open source web-based MySQL manager. You can find this pre-installed by many web providers (can be hard to find) or you can install it yourself.

    To copy the tables between databases using PHPMyAdmin, select the database you want to copy FROM using the dropdown menu on the left. Then select a table below that menu. Click the Operations tab at the top of the screen. You'll see the 'Copy table to...' box, in which you can select the master database that contains your users. Then next to that you have to type the unique header for that blog (from your 'wp-config.php' file) followed by an underscore and the original name of that table.

    For example, 'wp_categories' would be changed to 'wp_blog2_categories'.

    Leave the default settings, and click Go. Do this for every table *EXCEPT* 'wp_users'. I have not tried merging two user databases, and don't know for sure what will happen if you try. It might work for all I know, but if users have identical accounts on two of your site, this could confuse the database. If anyone has success with this, I'd be curious to know. At the very least, this method will retain your sites' content, though some users may have to re-register.

    That should be everything! Good luck to anyone else, and thanks to those who helped above. Sorry for the long post, but I hope someone finds this useful.

    Best,
    Jeremy

  8. dbasulto
    Member
    Posted 8 years ago #

    can this be done using a remote DB?
    i mean, having 2 blogs on 2 mysql servers, and connect the users table from one blog to another? (both servers are taking external connections)

  9. dbasulto
    Member
    Posted 8 years ago #

    actually, this wont work with WP2, since it stores user levels (capabilities) in a key inside a row of wp_usermeta. and it uses wp prefix..
    so it wont be that easy. i tried changing some code in capabilities.php in the 2nd install:

    $this->cap_key = $table_prefix . 'capabilities';

    with

    $this->cap_key = 'wp_capabilities';

    (being wp the prefix for the main blog)

    it still didnt work..since authors and editors cant work on the 2nd blog.
    it was added to WP ticketing system here:

    http://redalt.com/support/topic/46

    anyone has a temp fix for this?
    i guess while the 2nd blog loads the capabilities from $prefix-for-first-install_capabilities there shouldnt be a problem...

  10. guchuj05
    Member
    Posted 8 years ago #

    Line 73 of wp-settings.php is creating problems for me. This is unrelated to the discussion, but one of you may be able to help. My entire website http://atennisblog.com/ is down and giving this error:

    Warning: main(/home/guchuj05/public_html/wp-includes/wp-db.php): failed to open stream: Permission denied in /home/guchuj05/public_html/wp-settings.php on line 73

    Fatal error: main(): Failed opening required '/home/guchuj05/public_html/wp-includes/wp-db.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/guchuj05/public_html/wp-settings.php on line 73

    Any thoughts on what may be the problem? I haven't altered this file in any way.

    Thank you!

  11. yoshi
    Member
    Posted 8 years ago #

    Here is my answer with wp2.

    1st blog's table prefix : 'wp_'
    2nd blog's table prefix : 'wp2_'
    Both in the same DB.

    Step 1:

    After installation, in the 2nd blog's wp-settings.php, around line 87,
    change

    $wpdb->prefix = $table_prefix;

    to


    $table_prefix = 'wp_';
    $wpdb->prefix = $table_prefix;
    $wpdb->users = 'wp_users';
    $wpdb->usermeta = 'wp_usermeta';

    Step 2:

    In the wp2_options table in the database,
    look for an entry with wp2_user_roles in option_name column.
    Change this to wp_user_roles.

    That's it. This seems to be working for me.

  12. dbasulto
    Member
    Posted 8 years ago #

    the above method works fine, but users capabilities/user levels aren't shared across the 2 blogs. so users on the 1st blog, can login, and get an error message on the 2nd blog, and they aren't even able to comment... is there any way to share the capabilities across?

  13. yoshi
    Member
    Posted 8 years ago #

    If you are seeing "don't have permission" error after logged in, you provably have missed this part.

    In the wp2_options table in the database,
    look for an entry with wp2_user_roles in option_name column.
    Change this to wp_user_roles.

    If you have manipulated caps&roles using plugin this would be tricky though.

  14. garsan
    Member
    Posted 8 years ago #

    Hi, I have the lines

    define('CUSTOM_USER_TABLE','wp_users');
    define('CUSTOM_USER_META_TABLE','wp_usermeta');

    on my wp-config.php and modified the wp_user_roles column, but when i log-in on my second site i get the "You do not have sufficient permissions..." page, when I go to the Dashboard I get the normal user welcome with no admin options.

    I'm using WP 2.0.2 in both sites.

    Thanks for the help on this.

  15. humblecoder
    Member
    Posted 7 years ago #

    Hello,

    I'm not terribly familiar with WordPress as a whole. I'm used to *building* things like WP as opposed to actually making practical use of them. That aside, I'm curious as to the best manner by which to go about rolling out multiple sites using all of the same code. Like so:

    site1.mysite.com
    site2.mysite.com
    site3.mysite.com

    I want them to all use the same information, just a different "template". Getting Apache set up with virtual hosts and the like seems to be the easy part for me. But I can't seem to wrap my head around how to get WP to use a completely different template for each site while sharing 100% of the functionality and data. I'm sure it's amazingly simple, but I'm not seeing it.

    Thanks in advance!

  16. mkn
    Member
    Posted 7 years ago #

    Hi,

    Got it up and working by following the instructions by Yoshi. Thanks a lot!

    My question is this:

    Is it possible to make several subdomains share the same credentials/cookies as well? So that when logging in on one site you are automatically logged in on the rest of them as well?

    I've looked into the wp-settings.php and found some references to cookies, but my experience in php is veeery limited which makes it kind of hard to decrypt where even to start the hacking. :)

    Thanks in advance

    Mkn.

  17. misterseo
    Member
    Posted 7 years ago #

    RE: Sharing cookies across multiple sites. This can be accomplished by editing wp_settings.php
    $cookiehash = md5(get_settings('siteurl'));
    to
    $cookiehash = md5('put something consistent across all your sites here');

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags