Php 批量向WooCommerce用户列表添加新角色
我有一份141位客户的名单,他们从我们的WooCommerce网站购买了一种产品。我使用UserInsights插件()提取列表 我试图做的是,在这些用户拥有的任何角色的基础上,向他们批量添加一个不同的角色,如果他们已经拥有了新角色,那么就不要对特定的用户做任何事情 使用UserInsights()中的这篇文章,我尝试将以下代码添加到我的functions.php中,它显示代码已执行,但用户从未收到新角色 下面是我添加到functions.php的代码:Php 批量向WooCommerce用户列表添加新角色,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一份141位客户的名单,他们从我们的WooCommerce网站购买了一种产品。我使用UserInsights插件()提取列表 我试图做的是,在这些用户拥有的任何角色的基础上,向他们批量添加一个不同的角色,如果他们已经拥有了新角色,那么就不要对特定的用户做任何事情 使用UserInsights()中的这篇文章,我尝试将以下代码添加到我的functions.php中,它显示代码已执行,但用户从未收到新角色 下面是我添加到functions.php的代码: function uiwc_set_cu
function uiwc_set_custom_role() {
//the slug of our newly created group
$group_slug = 'homeschool-strong';
//the role we want to change our users to
$user_role = 'homeschool-strong';
//telling WP which taxonomy we're looking into
$taxonomy = 'usin_group';
//getting the UI group information
$term = get_term_by( 'slug', $group_slug, $taxonomy, 'ARRAY_A' );
// getting the users from that group
$id = $term['term_id'];
$out = get_objects_in_term( $id, $taxonomy );
//checking if there are any users
if( ! empty( $out ) ) {
//let's loop trhough all users
foreach ( $out as $uid) {
//get the user related to this ID
$user = new WP_User($uid);
//do not change the role of admins
if( !user_can($user, 'administrator') ){
//set the new role to our customer
$user->$user->add_role($role);
}
}
// just so you know the code worked
echo "<script>alert('code run');</script>";
}
}
add_filter('admin_footer', 'uiwc_set_custom_role');
函数uiwc\u set\u custom\u role(){
//我们新成立的团队的鼻涕虫
$group_slug=‘家庭学校强’;
//我们希望将用户更改为的角色
$user_role='homeschool strong';
//告诉WP我们正在研究哪种分类法
$taxonomy='usin_group';
//获取UI组信息
$term=get_term_by('slug',$group_slug,$taxonomy,'ARRAY_A');
//从该组获取用户
$id=$term['term_id'];
$out=get_objects_in_term($id,$taxonomy);
//检查是否有任何用户
如果(!空($out)){
//让我们循环测试所有用户
foreach($uid){
//获取与此ID相关的用户
$user=新的WP\u用户($uid);
//不要更改管理员的角色
如果(!user_can($user,'administrator')){
//为我们的客户设置新角色
$user->$user->add_角色($role);
}
}
//只是想让你知道代码是有效的
回显“警报(‘代码运行’);”;
}
}
添加过滤器(“管理员页脚”、“uiwc设置”自定义角色);
有人知道为什么在执行此代码时,新角色没有添加到用户中吗?或者您知道一种更好的方法将新角色添加到WooCommerce用户列表中?您的代码中有两个问题 这一行:
$user->$user->add_role($role);
应如下所示:
$user->add_role($user_role);
解释您正在使用该对象两次,并且没有定义声明为user\u role的变量role
第二期:
$user_role = 'homeschool-strong';
在您的声明中,WordPress将被视为数组而不是字符串,因此应如下所示:
$user_role = 'homeschool_strong';
所以完整的代码应该是这样的:
function uiwc_set_custom_role()
{
//the slug of our newly created group
$group_slug = 'homeschool-strong';
//the role we want to change our users to
$user_role = 'homeschool_strong';
//telling WP which taxonomy we're looking into
$taxonomy = 'usin_group';
//getting the UI group information
$term = get_term_by('slug', $group_slug, $taxonomy, 'ARRAY_A');
// getting the users from that group
$id = $term['term_id'];
$out = get_objects_in_term($id, $taxonomy);
//checking if there are any users
if (!empty($out)) {
//let's loop trhough all users
foreach ($out as $uid) {
//get the user related to this ID
$user = new WP_User($uid);
//do not change the role of admins
if (!user_can($user, 'administrator')) {
//set the new role to our customer
$user->add_role($user_role);
}
}
// just so you know the code worked
echo "<script>alert('code run');</script>";
}
}
add_filter('admin_footer', 'uiwc_set_custom_role');
函数uiwc\u set\u custom\u role()
{
//我们新成立的团队的鼻涕虫
$group_slug=‘家庭学校强’;
//我们希望将用户更改为的角色
$user_role='homeschool_strong';
//告诉WP我们正在研究哪种分类法
$taxonomy='usin_group';
//获取UI组信息
$term=get_term_by('slug',$group_slug,$taxonomy,'ARRAY_A');
//从该组获取用户
$id=$term['term_id'];
$out=get_objects_in_term($id,$taxonomy);
//检查是否有任何用户
如果(!空($out)){
//让我们循环测试所有用户
foreach($uid){
//获取与此ID相关的用户
$user=新的WP\u用户($uid);
//不要更改管理员的角色
如果(!user_can($user,'administrator')){
//为我们的客户设置新角色
$user->add_role($user_role);
}
}
//只是想让你知道代码是有效的
回显“警报(‘代码运行’);”;
}
}
添加过滤器(“管理员页脚”、“uiwc设置”自定义角色);
我刚刚测试了代码,它在kashalo的帮助下运行了最终运行的代码是:
function uiwc_set_custom_role()
{
//the slug of our newly created group
$group_slug = 'homeschool-strong';
//the role we want to change our users to
$user_role = 'homeschool-strong';
//telling WP which taxonomy we're looking into
$taxonomy = 'usin_group';
//getting the UI group information
$term = get_term_by('slug', $group_slug, $taxonomy, 'ARRAY_A');
// getting the users from that group
$id = $term['term_id'];
$out = get_objects_in_term($id, $taxonomy);
//checking if there are any users
if (!empty($out)) {
//let's loop trhough all users
foreach ($out as $uid) {
//get the user related to this ID
$user = new WP_User($uid);
//do not change the role of admins
if (!user_can($user, 'administrator')) {
//set the new role to our customer
$user->add_role($user_role);
}
}
// just so you know the code worked
echo "<script>alert('code run');</script>";
}
}
add_filter('admin_footer', 'uiwc_set_custom_role');
函数uiwc\u set\u custom\u role()
{
//我们新成立的团队的鼻涕虫
$group_slug=‘家庭学校强’;
//我们希望将用户更改为的角色
$user_role='homeschool strong';
//告诉WP我们正在研究哪种分类法
$taxonomy='usin_group';
//获取UI组信息
$term=get_term_by('slug',$group_slug,$taxonomy,'ARRAY_A');
//从该组获取用户
$id=$term['term_id'];
$out=get_objects_in_term($id,$taxonomy);
//检查是否有任何用户
如果(!空($out)){
//让我们循环测试所有用户
foreach($uid){
//获取与此ID相关的用户
$user=新的WP\u用户($uid);
//不要更改管理员的角色
如果(!user_can($user,'administrator')){
//为我们的客户设置新角色
$user->add_role($user_role);
}
}
//只是想让你知道代码是有效的
回显“警报(‘代码运行’);”;
}
}
添加过滤器(“管理员页脚”、“uiwc设置”自定义角色);
非常感谢您的回答。。。这肯定有帮助。起初,它不起作用,但后来我重新添加了旧的$user_角色,因为这实际上是它的鼻涕虫:$user_角色='homeschool strong';这就成功了!不客气,如果你喜欢我的回答,请接受:)