使用此代码,您将在注册期间检查用户的电子邮件,并附加您想要的角色:
<?php
add_action( \'user_register\', \'wp234_set_role_by_email\' );
function wp234_set_role_by_email( $user_id ){
$user = get_user_by( \'id\', $user_id );
$domain = substr(
strrchr(
$user->data->user_email,
"@"
), 1
); //Get Domain
$contributor_domains = array( \'gmail.com\' );
if( in_array( $domain, $contributor_domains ) ){
foreach( $user->roles as $role )
$user->remove_role( $role ); //Remove existing Roles
$user->add_role( \'contributor\' ); //Add role
}
$subscriber_domains = array( \'hotmail.com\' );
if( in_array( $domain, $subscriber_domains ) ){
foreach( $user->roles as $role )
$user->remove_role( $role ); //Remove existing Roles
$user->add_role( \'subscriber\' ); //Add role
}
}
?>
如果您希望其他所有电子邮件域都是订阅者,只需进入“管理”>“设置”并将通常的角色声明为“订阅者”。或者,您可以使用此
$subscriber_domains
-零件:
<?php
if( ! in_array( $domain, $contributor_domains ) ){
foreach( $user->roles as $role )
$user->remove_role( $role ); //Remove existing Roles
$user->add_role( \'subscriber\' ); //Add role
}
?>