基本上,您所需要做的就是添加一个名为role的隐藏字段,并将该值设置为您想要在注册表表单上的任何角色。
add_action(\'register_form\',\'show_role_field\');
function show_role_field(){ ?>
<input id="role" type="hidden" tabindex="20" size="25" value= "<?php if (isset($_GET[\'role\'])){echo $_GET[\'role\'];} ?>" name="role"/>
<?php
}
下一步是在用户提交注册表时注册该角色。
add_action(\'user_register\', \'register_role\');
function register_role($user_id, $password="", $meta=array()) {
$userdata = array();
$userdata[\'ID\'] = $user_id;
$userdata[\'role\'] = $_POST[\'role\'];
//only allow if user role is my_role
if ($userdata[\'role\'] == "my_role"){
wp_update_user($userdata);
}
}
现在剩下要做的就是将用户定向到register url,例如,您想要作为查询变量的角色
http://example.com/wp-login.php?action=register&role=my_role
你可以阅读更多
http://www.jasarwebsolutions.com/2010/06/27/how-to-change-a-users-role-on-the-wordpress-registration-form/我建议您在保存之前检查是否允许注册该角色,这样您就不会在您的站点中获得一些新管理员。