Change user role on registration

This topic contains 6 reply and 2 voices, and was last updated by nonameolsson 11 years, 3 months ago
Viewing 6 Posts - 1 through 6 (of 6 total)
Author Posts
August 24, 2013 at 12:57 am 7395
nonameolsson Hello! I've been reading the documentation, and it really a good one! But I can't get this to work, and I have read the explanation in another thread: http://wedevs.com/support/topic/choose-user-type-on-registration/#post-2403 I have four user roles: Administrator(WP admin), Administration, Full time and Part time. On registration the choice is between: Administrator, Full time and Part time. I'm using the code example but can't get it to work. This is my code in my functions.php: [php] function wpufe_my_switch_reg( $args ) { if ( $_POST['membertype'] == 'Deltid' ) { $args['role'] = 'parttime'; } elseif ( $_POST['membertype'] == 'Heltid' ){ $args['role'] = 'fulltime'; } elseif ( $_POST['membertype'] == 'Administration' ){ $args['role'] = 'administration'; } return $args; } add_filter( 'wpuf_register_user_args', 'wpufe_my_switch_reg' ); [/php] In the form I use a drop down with the meta key: membertype. The preset values are: Heltid, Deltid and Administration. What am I doing wrong? I tried adding, after the elseif-arguments, add an else. I noticed that it always enters the last else statement. So I think that I'm doing something wrong, since it never enters some of the first statements. I appreciate all help available!
August 24, 2013 at 1:34 am 7396
Tareq Hasan Tareq Hasan

The code seems fine, possibly the problem is with the user role names. Please check if you are giving the correct role names. In default WordPress, the role names are all small caps: administrator, editor, author, subscriber, contributor.

August 24, 2013 at 2:03 am 7400
nonameolsson nonameolsson

Thanks for a quick reply!
I attach a screenshot with the user roles. I’ve added them using the Members plugin, http://wordpress.org/plugins/members/.
https://docs.google.com/file/d/0B1D8Th-Je30Ed2FpcUk1UEhEdWs/

August 25, 2013 at 2:00 am 7462
Tareq Hasan Tareq Hasan

The names looks okay. Check another thing, goto add new user page in admin and see the select dropdown of user role. The option values should be correct.

August 25, 2013 at 2:38 am 7465
nonameolsson nonameolsson

I did that. The drop down options have the same value as in the function. I tried with a new WordPress installation also, with the out-of-the-box user roles. But it doesn’t work either.
If you try it yourself, does it work?

Here are screenshots of my current new WordPress installation.
https://drive.google.com/folderview?id=0B1D8Th-Je30Eb3RKdHJHMzA3aVk&usp=sharing

I can’t understand what I’m doing wrong here. I’ve been trying this for hours now! I appreciate all the help! Thanks for great support!

August 25, 2013 at 3:00 am 7466
Tareq Hasan Tareq Hasan

The problem is, the $_POST['membertype'] is an array. Thats why it’s not matching your if/else block. Here’s what I tried (using anonymous function, but you get the idea):

[php]
add_filter( ‘wpuf_register_user_args’, function( $args ) {

if ( !isset( $_POST[‘user_role’] ) ) {
return $args;
}

// get the first element of the array
$user_role = reset( $_POST[‘user_role’] );

switch ( $user_role ) {
case ‘Administrator’:
$args[‘role’] = ‘administrator’;
break;

case ‘Subscriber’:
$args[‘role’] = ‘subscriber’;
break;

case ‘Shop Worker’:
$args[‘role’] = ‘shop_worker’;
break;

case ‘Shop Manager’:
$args[‘role’] = ‘shop_manager’;
break;
}

return $args;
});
[/php]

August 26, 2013 at 6:17 pm 7493
nonameolsson nonameolsson

Thank you very much! Now it works as it should!

Viewing 6 Posts - 1 through 6 (of 6 total)