在共享相同wp_users和wp_usermeta表的两个Wordpress安装之间同步所有用户角色. [英] Sync all User Roles between two Wordpress Installs sharing the same wp_users and wp_usermeta tables.

查看:141
本文介绍了在共享相同wp_users和wp_usermeta表的两个Wordpress安装之间同步所有用户角色.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个共享相同用户(共享相同的wp_users和wp_user_meta表)的Woocommerce商店(数据库前缀wp_)和Wordpress Blog(数据库前缀wp_new _).

I have created a Woocommerce Store (database prefix wp_) and a Wordpress Blog (database prefix wp_new_) that share the same users (sharing the same wp_users and wp_user_meta tables).

我不仅要同步用户,还要同步所有用户的用户角色(多个用户角色).

I want to sync not just users but also user roles (multiple user roles) of all users.

为此,我尝试了> https://kinsta.com/blog/提供的解决方案share-logins-wordpress/

    function ksu_save_role( $user_id, $role ) {

        $prefix_1 = 'wp_'; 
        $prefix_2 = 'wp_new_'; 

        $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( 'set_user_role', 'ksu_save_role', 10, 2 );

当仅将单个用户角色分配给用户时,上述解决方案将非常有用.但是,如果为一个用户分配了多个用户角色,那么它将不起作用.我的意思是,它不会同步所有用户角色.

The above solution works great when only a single user role is assigned to a user. But if a user is assigned multiple user roles then it doesn’t work. I mean, it doesn’t sync all user roles.

在浏览数据库之后,我了解到解决方案在于将"wp_capabilities"的"meta_value"克隆为"wp_new_capabilities"(在wp_usermeta中)

After digging through the database I understood that the solution lies in cloning the "meta_value" of "wp_capabilities" to "wp_new_capabilities" (in wp_usermeta)

是否可以将"user_id"的整个"meta_value"从"wp_capabilities"复制到"wp_new_capabilities"?

Is there a way to copy entire ‘meta_value’ for a ‘user_id’ from "wp_capabilities" to "wp_new_capabilities"?

如果我们可以将整个meta_value从"wp_capabilities"复制到"wp_new_capabilities",则可以同步分配给用户的所有用户角色.

If we can copy the entire meta_value from "wp_capabilities" to "wp_new_capabilities" then all the user roles assigned to a user can be synced.

那么要对上述代码进行什么更改才能实现此目的?

So what changes need to be done to the above mentioned code to achieve this?

谢谢!

推荐答案

我在同步用户角色(每个用户有多个用户角色)时遇到问题. 在将午夜的油燃烧了两个多晚上之后,我发现了一种愚蠢的神奇解决方案:)

I had issues with syncing user roles (multiple user roles per user). After burning midnight oil for more than two nights I found the silly magical solution :)

我只是在"add_action('set_user_role','ksu_save_role',10,2);"中将'set_user_role'更改为'add_user_role';

I simply changed 'set_user_role' to 'add_user_role' in "add_action( 'set_user_role', 'ksu_save_role', 10, 2 );"

小魔术调整后的结束代码

The end code after the small magical tweak

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 ); // THE MAGIC MODIFICATION

代码积分: https://kinsta.com/blog/share-logins-wordpress /

将其添加到functions.php中,您就很高兴了.

Add this to functions.php and you are great to go.

它与角色转换插件兼容,例如"Woocommerce订阅"和"YITH WooCommerce Premium的自动角色转换器"

It is compatible with role changer plugins like "Woocommerce Subscriptions" and "YITH Automatic Role Changer for WooCommerce Premium"

您可以根据需要设置和更改多个角色.

You can set and change as many roles as you want.

这篇关于在共享相同wp_users和wp_usermeta表的两个Wordpress安装之间同步所有用户角色.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆