REGISTER WITH A ROLE.
Quiero hacer esto en bonos.biz para dar el link dependiendo de que bonos quiero dar, y cuando se registren automaticamente se les asigne el role. Apenas lo voy a probar, y es en la function.php de ese theme.
1. Add a field to the registration form. I am going to make this a hidden field so that it will not distract from the other fields. Also, I am grabbing the role from the URL with the $_GET variable. This is so I could have different registrations forms for different types of users. (For example: URL is http://example.com/wp-login.php?action=register&role=my_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
}
2. Next thing is to register that role when the user has submitted the registration form.
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);
}
}
You will obviously have to change my_role to the role you want them to register for. Also because we are taking the role from a $_GET variable we can modify this form so that other roles are passed to it. If you want to have more than one role allowed to be registered then change the if statement to:
//allow other roles
if (
($userdata['role'] == "my_role1")or
($userdata['role'] == "my_role2")or
($userdata['role'] == "my_role3")
){
wp_update_user($userdata);
}
As a security note, you DO NOT want to allow for a role with administration rights. This is why the if statement above is so important to make sure a user will not register as an administrator.
____________________________________________________
También probar esta: y ver si puedo hacer un plugin con dos parametros:
//create a hidden field for role
add_action('register_form','wpse_add_hidden_role_field');
function wpse_add_hidden_role_field(){
if ( isset( $_GET[ 'role' ] ) ){
echo '<input id="user_role" type="hidden" tabindex="20" size="25" value="' . $_GET[ 'role' ] . '" name="role"/>';
}
}
//validate we have permitted roles if not, don't allow subscription
add_filter( 'registration_errors', 'wpse_role_check' );
function wpse_role_check( $errors ){
if( isset( $_POST[ 'role' ] ) ) {
//only allow registration if roles are in this array.
$permitted_roles = array(
'buyer',
'seller',
);
if( ! in_array( $_POST[ 'role' ], $permitted_roles ) ){
$errors->add( 'role_not_allowed', __( '<strong>ERROR</strong>: This registration is not allowed.', 'my_textdomain' ) );
}
}
// Else disallow public registration (i.e. no role query string found )
// If you don't take this into account, anyone can register as subscribers
else {
$errors->add( 'public_not_allowed', __( '<strong>ERROR</strong>: Public registration is not allowed.', 'my_textdomain' ) );
}
return $errors;
}
//update user profile that have passed registration validation
add_action('user_register', 'wpse_update_role');
function wpse_update_role( $user_id ) {
if ( isset( $_POST[ 'role' ] ) ){
$userdata = array();
$userdata[ 'ID' ] = $user_id;
$userdata[ 'role' ] = $_POST[ 'role' ];
wp_update_user( $userdata );
}
}