0

I have created two Wordpress installs 1) Master 2) Slave

The two installs, Master and Slave, share the same database.

I have followed the steps mentioned in this article: "How to Share Logins and Users Between Multiple WordPress Sites" https://kinsta.com/blog/share-logins-wordpress/

  • The database prefix for the first install Master is "wp_master_"
  • The database prefix for the second install Slave is "wp_slave_"

The two installs share the same user base "wp_master_users" and "wp_master_usermeta".

In "wp_master_usermeta" table, the user capabilities are stored in

  • "wp_master_capabilities" Row for the Master, and
  • "wp_slave_capabilities" Row for the Slave

But I want the Slave to use the "wp_master_capabilities" instead of "wp_slave_capabilities"

That is, the user roles on both the installs should be same as that of the Master.

How can this be achieved?

2 Answers2

1

I've never done this before and maybe Wordpress has hooks to archive this table-change, but it should be possible to do this on mysql-side too, which might be even a better solution.

You could ..

  1. use federated storage ( https://stackoverflow.com/a/24532395/10362812 )
    This is my favorite, because you don't even have to share a database or even the mysql server
    The downside is, that it doesn't work with db cache and uses an additional connection.
  2. create a view ( https://stackoverflow.com/a/1890165/10362812 )
    This is the simplest solution and fits to your configuration.
    Downside: The 2 tables have to be in the same database / be assigned to the same user at least.

    -- **Backup your database before trying!** --
    DROP TABLE wp_slave_users;
    DROP TABLE wp_slave_usermeta;
    CREATE VIEW wp_slave_users AS SELECT * FROM wp_master_users;
    CREATE VIEW wp_slave_usermeta AS SELECT * FROM wp_master_usermeta;
    
  3. create a shadow copy ( https://stackoverflow.com/a/1890166/10362812 )
    Works with caching and is a standalone table
    Downsides as 2. solution + a bit of setup and I think it might be the worst option in performance

This are all answers to this question: How do I create a table alias in MySQL

Let me know if this works, I have to do a similar task next week. While researching I found your question and the linked answers.

Greetings, Eric!

  • Hello Eric, thanks for the answer. Case 2 is something that comes close to my scenario. The two tables are in the same database. The SQL query that you have given creates two tables wp_master_users and wp_slave_usermeta. But that's not what I am looking at. I want the Slave to use the "wp_master_capabilities" instead of "wp_slave_capabilities". Both Rows "wp_master_capabilities" and "wp_slave_capabilities" are within the table "wp_master_usermeta". I am looking for an SQL query that can achieve this. Thanks, however, you have given it a direction. – Manjunath Pmf Ias Sep 14 '18 at 15:37
  • It's not creating 2 tables. It's creating 2 views,... like aliases for the master tables. So this should work. – Eric Marten Sep 15 '18 at 14:59
  • Hello Eric, I have used a different approach, and it is working great. I have posted it as an answer here. Thanks for your efforts. – Manjunath Pmf Ias Sep 17 '18 at 13:29
  • Thanks, but due to the hook-names that seems to work only for changing a userrole and for assigning them while login, but not for adding a user, updates on the user etc. or what did you exactly achieve with this solution? Greets – Eric Marten Sep 24 '18 at 12:28
0

I placed the below code in functions.php of both Master & Slave to achieve the functionality described in the question.

(The entire procedure is carried out as described in this post How to Share Logins and Users Between Multiple WordPress Sites)

// Sync User Roles Between Master & Slave WordPress installs that share the same database

function ksu_save_role1( $user_id, $role ) {

$prefix_1 = 'wp_master_'; Table Prefix of Store (Master) 
$prefix_2 = 'wp_slave_'; Table Prefix of Blog (Slave)

$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_role1', 10, 2); 
add_action( 'wp_login', 'ksu_save_role1', 10, 2);