admin管理员组文章数量:1025206
I'm trying to add an additional role for a user when they register if they register as a specific role.
Here is my function:
function add_secondary_role( $user_id ) {
$current_user = wp_get_current_user();
if ( in_array( 'um_dueling-pianist', (array) $current_user->roles ) ) {
$user = get_user_by('id', $user_id);
$user->add_role('pianist');
}
}
It doesn't work. I have registered a test account with the role 'um_dueling-pianist' and it does not add the other role of 'pianist.' Those are both the meta values for the user roles and not the display name of the user roles.
Any idea what's wrong with my function?
I'm trying to add an additional role for a user when they register if they register as a specific role.
Here is my function:
function add_secondary_role( $user_id ) {
$current_user = wp_get_current_user();
if ( in_array( 'um_dueling-pianist', (array) $current_user->roles ) ) {
$user = get_user_by('id', $user_id);
$user->add_role('pianist');
}
}
It doesn't work. I have registered a test account with the role 'um_dueling-pianist' and it does not add the other role of 'pianist.' Those are both the meta values for the user roles and not the display name of the user roles.
Any idea what's wrong with my function?
Share Improve this question asked Apr 7, 2019 at 19:50 osakagregosakagreg 1351 silver badge4 bronze badges1 Answer
Reset to default 1You check the role not for the created user, only the currently logged in user. Try this way:
add_action( 'user_register', 'se333727_additional_roles' );
function se333727_additional_roles( $user_id )
{
$new_user = get_user_by( 'ID', $user_id );
if ( in_array( 'um_dueling-pianist', (array)$new_user->roles ) ) {
$new_user->add_role('pianist');
}
}
I'm trying to add an additional role for a user when they register if they register as a specific role.
Here is my function:
function add_secondary_role( $user_id ) {
$current_user = wp_get_current_user();
if ( in_array( 'um_dueling-pianist', (array) $current_user->roles ) ) {
$user = get_user_by('id', $user_id);
$user->add_role('pianist');
}
}
It doesn't work. I have registered a test account with the role 'um_dueling-pianist' and it does not add the other role of 'pianist.' Those are both the meta values for the user roles and not the display name of the user roles.
Any idea what's wrong with my function?
I'm trying to add an additional role for a user when they register if they register as a specific role.
Here is my function:
function add_secondary_role( $user_id ) {
$current_user = wp_get_current_user();
if ( in_array( 'um_dueling-pianist', (array) $current_user->roles ) ) {
$user = get_user_by('id', $user_id);
$user->add_role('pianist');
}
}
It doesn't work. I have registered a test account with the role 'um_dueling-pianist' and it does not add the other role of 'pianist.' Those are both the meta values for the user roles and not the display name of the user roles.
Any idea what's wrong with my function?
Share Improve this question asked Apr 7, 2019 at 19:50 osakagregosakagreg 1351 silver badge4 bronze badges1 Answer
Reset to default 1You check the role not for the created user, only the currently logged in user. Try this way:
add_action( 'user_register', 'se333727_additional_roles' );
function se333727_additional_roles( $user_id )
{
$new_user = get_user_by( 'ID', $user_id );
if ( in_array( 'um_dueling-pianist', (array)$new_user->roles ) ) {
$new_user->add_role('pianist');
}
}
本文标签: functionsAdding additional roles on registration
版权声明:本文标题:functions - Adding additional roles on registration 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745617701a2159390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论