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/
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.
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 );