admin管理员组文章数量:1300054
Im Trying to set an user role. if an specific user role (sales-agents) create an user.
the created user should have the role b2b instead of customer
here is my try:
add_action( 'init', 'b2b_set_user_role' );
function b2b_set_user_role() {
if ( is_user_logged_in() && wcb2bsa_has_role( get_current_user_id(), 'sales_agent' ) )
{
$user->remove_role( ‘customer’ );
$user->add_role( ‘b2b’ );
}
}
i hope someone can help me
Im Trying to set an user role. if an specific user role (sales-agents) create an user.
the created user should have the role b2b instead of customer
here is my try:
add_action( 'init', 'b2b_set_user_role' );
function b2b_set_user_role() {
if ( is_user_logged_in() && wcb2bsa_has_role( get_current_user_id(), 'sales_agent' ) )
{
$user->remove_role( ‘customer’ );
$user->add_role( ‘b2b’ );
}
}
i hope someone can help me
Share Improve this question asked Mar 16, 2021 at 8:04 d3mid3mi 132 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0You can use user_register
action, which is invoked after registering a new user.
To add or remove roles/caps, the WP_User
class provides methods:
add_cap()
,add_role()
remove_cap()
,remove_role()
Your code might look like this:
add_action( 'user_register', 'se385135_user_register' );
function se385135_user_register( $user_id )
{
$user = wp_get_current_user();
if ( !isset( $user->ID ) || $user->ID == 0 )
return;
if ( !in_array( 'sales_agent', $user->roles ) )
return;
$created_user = get_user_by( 'ID', $user_id );
if ( $created_user === false )
return;
$created_user->add_role( 'b2b' );
$created_user->remove_role( 'customer' );
}
本文标签: initSet user roleif an specific role created an user
版权声明:本文标题:init - Set user role, if an specific role created an user 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741654719a2390691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$user
from? You can't just pull variables out of thin air – Tom J Nowell ♦ Commented Mar 25, 2021 at 13:43