0

I am currently working on two WordPress installations with the PointFinder theme. One is the main website (website A), whereas the other is a sub website accessible through the main website (website B).

The goal is, that the website A shares it's user information (users, usermeta, comments) with website B.

Website A
   |_ Website B (this one should access the user info from website A)

I already studied the database schema and some of the php files. Theoretically, I could define a second database connection in 'wp-config.php' and point every call to the users, usermeta and comments tables within the website B's php files to the database from website A.

But this seems quite extensive and error-prone. Any other ideas how to solve this issue?

UPDATE/SOLUTION:

As codiiv proposed in his answer below, sharing user/meta data between different WP sites is quite easy. Don't forget to add the script that updates the missing user roles when new users are added. Here's the most comprehensive link I found:

https://kinsta.com/blog/share-logins-wordpress/

salocinx
  • 3,715
  • 8
  • 61
  • 110

2 Answers2

1

1) To share users and usermeta information you can add the following lines of code in your secondary (sub site) wp-config.php file

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

You will need to replace the wp_ with the right prefix

2) For Comments, or other tables, I haven't done enough research but I think you can achieve that as long as the two are on the same server. Or you can create a custom RSS feed for comments(that include the full comment), and parse that in the subsite.

PHPer
  • 637
  • 4
  • 19
  • Thanks a lot for your suggestion. I will report back, as soon as I have tested your approach. – salocinx Jan 10 '17 at 10:25
  • The best way to dynamize the prefix is global $wpdb; $prefix=$wpdb->prefix; – PHPer Jan 10 '17 at 10:34
  • Do you mean in combination with the approach you described in your answer? Or as another approach by modifying all the database queries in the php files? – salocinx Jan 10 '17 at 17:35
  • I meant instead of hardcoding the users and metauser table names. So instead of `wp_users` it s best to use the dynamic way because you may have a different prefix than wp_ – PHPer Jan 10 '17 at 18:35
  • Ok it works now :-) But there have been some additional tricks to accomplish. Adding some script to functions() in order to update the missing roles when new users are added for example. Here's the most comprehensive link I found: https://kinsta.com/blog/share-logins-wordpress/ – salocinx Jan 13 '17 at 16:49
0

Sync all User Roles between two Wordpress Installs sharing the same wp_users and wp_usermeta tables.

function ksu_save_role( $user_id, $role ) {

    // Site 1
    // Change value if needed
    $prefix_1 = 'first_';

    // Site 2 prefix
    // Change value if needed
    $prefix_2 = 'second_';

    $caps = get_user_meta( $user_id, $prefix_1 . 'capabilities', true );
    $level = get_user_meta( $user_id, $prefix_1 . 'user_level', true );

    if ( $caps ){
        update_user_meta( $user_id, $prefix_2 . 'capabilities', $caps );
    }

    if ( $level ){
        update_user_meta( $user_id, $prefix_2 . 'user_level', $level );
    }
}

add_action( 'add_user_role', 'ksu_save_role', 10, 2 );