php - sharing user on 2 WordPress installs in 2 sub domains on same server -
i have 2 wordpress sites in 2 different sub-domains test1.abc.com , test2.abc.com . both sites have wp-require plugin activated , logged-in users can see site. want make system if user logged 1 site, should auto-login other one.
what try :
after searching know need use 1 database both sites. have done these steps:
i have download whole database of test2.abc.com site , change prefix wp_ wpmo_, replaced in whole database , upload first site's database. added these 2 lines in wp-config.php of second site, define second site should use first site's user table not own.
define('custom_usermeta_table', 'wp_usermeta'); define('custom_user_table', 'wp_users');
now, second site using first site's users , able login second site user details of first site.
the next problem cookies, added these lines in wp-config of both sites.
define('cookie_domain', '.abc.com'); define('cookiepath', '/'); define('cookiehash', 'aee53c017c29dc0d3ae37253fc8cbfd8');
now logged in test1.abc.com , when go test2.abc.com , asks me login. means cookies not passing first site second one. however, tried print $_cookie , gives me same encrypt values, user still not auto login on second site. when came first site , automatic logout. feel both sites related somehow on cookies , close still not reached goal of auto login second site.
any help?
solution :
after mikk3lro , others, have managed solve issue. posting solution faces same problem. here can find step step guide :
step 1: use 1 database both installations, install 2 wp using 2 different prefix on installation time.
step 2: make sure randomly generated secret keys , salts identical in both wp-config.php files.
step 3: paste these 2 lines in wp-config.php of second site.
//share user tables define('custom_user_meta_table', 'site1_prefix_usermeta'); define('custom_user_table', 'site1_prefix_users');
step 4: share cookies these lines . (write in both wp-config.php)
//share cookies define('cookie_domain', '.abc.com'); define('cookiehash', 'aee53c017c29dc0d3ae37253fc8cbfd8');
step 5: able auto login in second site when logged first site. error message on second site "you not have permission access page", thing.
step 6: reason is, wordpress checks user capability (wp-includes/capabilities.php) either have directly add capability in database (in case have few users) or write plugin this. @mikk3lro writes plugin in comments, good.
thanks
alright - close, there few more things done.
all requirements follows:
- share same database, using different prefixes - you've done this. here assume prefixes
wp1_
,wp2_
, on. - share
wp1_users
,wp1_usermeta
tables - you've done - , have overcome obstacle if had spelled constants name correctly... it'scustom_user_meta_table
(one more underscore have) - share cookies between subdomains using common
cookie_domain
,cookiehash
- you've done this - make sure (normally) randomly generated secret keys , salts identical - don't write you've done this, judging results think either have or keys empty (which not good, work)
- make sure there's
prefix_capabilities
entry each user each site in sharedusermeta
table - don't think you've done this, because haven't yet reached point realize it's necessary.
complete solution:
this goes in wp-config.php
:
//share user tables define('custom_user_meta_table', 'wp1_usermeta'); define('custom_user_table', 'wp1_users'); //share cookies define('cookie_domain', '.abc.com'); define('cookiehash', 'aee53c017c29dc0d3ae37253fc8cbfd8'); /** * in case these not needed - may if 1 * of installs located in sub-folder. have not tested * theory though. */ //define('cookiepath', '/'); //define('sitecookiepath', '/'); //define('admin_cookie_path', '/wp-admin'); //these need identical define('auth_key', 'this should random'); define('secure_auth_key', 'this should random string'); define('logged_in_key', 'one more random string'); define('auth_salt', 'oh - many random strings'); define('secure_auth_salt', 'salt, salt, salt , no eggs'); define('logged_in_salt', 'this sooooo random'); /** * these not need shared - in fact shouldn't * - if (in theory) actions on 1 site * intended other - not big concern in reality */ define('nonce_key', 'these should random too, can differ'); define('nonce_salt', 'one site has one, other another');
this enough logged in on both sites - there's still last annoying bullet left on list.
the problem permissions ("capabilities") on 1 of sites because meta_key
prefixed table prefix of site. if google around bit find lots of solutions recommending modify wp-includes/capabilities.php
use common prefix instead - recommend against that! (not security reasons, because need make patch / hack after every update... , it's insanely bad practice modify core files)
instead remedy obstacle need duplicate wp1_capabilities
row in wp1_usermeta
table (for each user!), giving new umeta_id
, substituting table prefix wp1_
wp2_
in meta_key
column. need each site, have 1 row meta_key
wp1_capabilities
, 1 wp2_capabilities
, on.
if , friend of yours users who'll ever log in sites, hand through phpmyadmin or - if need work dynamically, should quite possible automate small plugin (see edit below).
i've hated design - table prefix has no business inside table row! think needed multisite installs, though i'm sure there other (better) ways solve it...
update: plugin keep user roles synchronized between sites
this simple plugin duplicate , keep required rows in usermeta
table updated when users created or edited.
one thing worth noting won't work multisite installs because have special capabilities / roles. have not tested this.
it may need refinement specific use cases (please comment), job fine limited test case includes few users. inefficient site thousands of users, runs when user modified, , updates if needed doubt major concern. should relatively easy adapt read , modify user added / edited. complicate initial setup bit though, pre-existing users not automatically duplicated on first run.
create folder wp-content/plugins/duplicate-caps
, inside put following in duplicate-caps.php
- , don't forget activate under plugins
in wordpress admin. needs installed on sites.
<?php /* plugin name: duplicate caps plugin uri: description: tiny plugin duplicate capabilities in setup users (and user tables) shared across more 1 install author: mikk3lro version: 0.1 author uri: */ $dummy = new duplicate_caps(); class duplicate_caps { function __construct() { add_action('updated_user_meta', array($this, 'update_prefixed_caps'), 10, 2); add_action('added_user_meta', array($this, 'update_prefixed_caps'), 10, 2); add_action('deleted_user_meta', array($this, 'update_prefixed_caps'), 10, 2); } function update_prefixed_caps($mid, $object_id) { /** * note $object_id contains id of user * changed. * on site many users make sense * , set information regarding just-changed user * function corrects roles users * making sure pre-existing users duplicated, , keeping * table in sync. */ global $wpdb; //quick , dirty - *capabilities rows users $sql = "select * {$wpdb->usermeta} `meta_key` '%capabilities'"; $results = $wpdb->get_results($sql) or die(mysql_error()); //will hold prefixes (always include our own) $prefixes = array($wpdb->prefix); //will grab existing role each prefix $user_roles = array(); //loop our results foreach ($results $result) { //make sure meta_key looks right, , grab prefix if (preg_match('#^(.*)capabilities$#', $result->meta_key, $matches)) { $prefix = $matches[1]; // collect prefixes $prefixes[] = $prefix; //note entire row later use $user_roles[$result->user_id][$prefix] = $result; } } //make sure have 1 of each $prefixes = array_unique($prefixes); //loop through users found foreach ($user_roles $user_id => $existing_prefixes) { if (!isset($existing_prefixes[$wpdb->prefix])) { //user deleted - rows deleted //wordpress though, no cleanup :) } else { //we want prefixes obey (we created or changed //the user, want affect sites) $desired_role = $existing_prefixes[$wpdb->prefix]->meta_value; //loop through prefixes foreach ($prefixes $prefix) { //data inserted / updated $cap_data = array( 'user_id' => $user_id, 'meta_key' => $prefix . 'capabilities', 'meta_value' => $desired_role ); //if prefix doesn't exist user if (!in_array($prefix, array_keys($existing_prefixes))) { //actually insert (user created) $wpdb->insert($wpdb->usermeta, $cap_data, array('%d', '%s', '%s')); } else if ($desired_role !== $existing_prefixes[$prefix]->meta_value) { //update if not correct (user edited) $cap_data['umeta_id'] = $existing_prefixes[$prefix]->umeta_id; $wpdb->replace($wpdb->usermeta, $cap_data, array('%d', '%s', '%s', '%d')); } } } } } }
Comments
Post a Comment