Home Forums Plugin Support WP User Frontend Pro Change user role on registration

This topic is: resolved

This topic contains 6 replies, has 2 voices, and was last updated by  nonameolsson 5 years, 9 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #7395

    nonameolsson
    Participant
    Post count: 10

    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!

    #7396

    Tareq Hasan
    Keymaster
    Post count: 2831

    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.

    #7400

    nonameolsson
    Participant
    Post count: 10

    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/

    #7462

    Tareq Hasan
    Keymaster
    Post count: 2831

    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.

    #7465

    nonameolsson
    Participant
    Post count: 10

    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!

    #7466

    Tareq Hasan
    Keymaster
    Post count: 2831

    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]

    #7493

    nonameolsson
    Participant
    Post count: 10

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

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Change user role on registration’ is closed to new replies.