• Resolved makmerghen

    (@makmerghen)


    hello
    in case using add_filter( ‘um_registration_for_loggedin_users’, ‘__return_true’ );
    to let the sign in user can add new users using UM registration form
    is it possible to determined the number of new users that specific user role can add ?

    as example if the user has “editor” role then he can add only 20 user and if he tried to add more he will find message saying you are not allow to add more users
    is that possible and how to do that?
    best regards

Viewing 15 replies - 16 through 30 (of 37 total)
  • missveronica

    (@missveronicatv)

    @makmerghen

    You can try this modified code:

    // Add current logged-in user as a referer
    add_action( 'um_registration_complete', 'um_110821_user_referral' ,1 );
    function um_110821_user_referral( $user_id ){
        $referer_id = get_current_user_id();
        update_user_meta( $user_id, "um_user_referral", $referer_id );
    }
    
    // Validate registration limit
    add_action("um_submit_form_register","um_110821_validate_registration_limit", 1 );
    function um_110821_validate_registration_limit( $post_form ){
      
        if(  isset( $post_form['user_email'] ) && ! empty( $post_form['user_email'] ) ){
            
            $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => get_current_user_id(),
                    "fields" => "ids"
                    )
                )
            );
            
            if( "membership30" == um_user("role") ){
               if( $users > 30 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           }elseif( "membership50" == um_user("role") ){
               if( $users > 50 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           }elseif( "membership100" == um_user("role") ){
               if( $users > 100 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           }elseif( "disabled_membership" == um_user("role") ){
                 UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
           }else{
                // no limit
           }
        }  
    
    }
    Thread Starter makmerghen

    (@makmerghen)

    Hi @missveronicatv @champsupertramp thank you the helping
    I tried the modified code and code snippets plugin accept it without error but it didn’t work in the next test
    I changed role disabled_membership to be administrator and then I tried to register new user and the result was that no limitation happened and the new user was created normally
    regards

    • This reply was modified 3 years ago by makmerghen.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Please provide the modified code so we can review it.

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    its existed here in the previous message by @missveronicatv
    I will copy it here

    // Add current logged-in user as a referer
    add_action( 'um_registration_complete', 'um_110821_user_referral' ,1 );
    function um_110821_user_referral( $user_id ){
        $referer_id = get_current_user_id();
        update_user_meta( $user_id, "um_user_referral", $referer_id );
    }
    
    // Validate registration limit
    add_action("um_submit_form_register","um_110821_validate_registration_limit", 1 );
    function um_110821_validate_registration_limit( $post_form ){
      
        if(  isset( $post_form['user_email'] ) && ! empty( $post_form['user_email'] ) ){
            
            $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => get_current_user_id(),
                    "fields" => "ids"
                    )
                )
            );
            
            if( "membership30" == um_user("role") ){
               if( $users > 30 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           }elseif( "membership50" == um_user("role") ){
               if( $users > 50 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           }elseif( "membership100" == um_user("role") ){
               if( $users > 100 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           }elseif( "disabled_membership" == um_user("role") ){
                 UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
           }else{
                // no limit
           }
        }  
    
    }

    regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Did you change this part?

    from:

    elseif( "disabled_membership" == um_user("role") ){
                 UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
    }

    to:

    elseif( "administrator" == um_user("role") ){
                 UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
     }

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    yes I did

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    With this code, it should show the error when an administrator tries to Register a new account:

    elseif( "administrator" == um_user("role") ){
                 UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
     }

    Are you using an administrator account to test this? How many roles are assigned to your test admin account?

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    I found where is the issue but don’t know how to fix please help

    when doing the registration by fill username and email address with data I got the error and me as admin didn’t be able to create new user

    but
    when I fill only the username and left email empty and continue registration at this case the registration didn’t stopped and the new user was created

    please note that I am using code you provide me to fill email field automatically with dummy email in case it was left empty

    add_action("um_submit_form_register","um_111121_email_field_optional", 1 );
    function um_111121_email_field_optional(){
        
        if( isset( UM()->form()->errors['user_email'] ) && UM()->form()->errors['user_email'] == "You must provide your email" ){
            if( count( UM()->form()->errors ) == 1 ){
                unset( UM()->form()->errors );
            }
            unset( UM()->form()->errors['user_email'] );
            
        }
    }

    so please advise

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Please try this code snippet:

    add_action("um_submit_form_register","um_111121_email_field_optional", 1 );
    function um_111121_email_field_optional(){
        
        if( isset( UM()->form()->errors['user_email'] ) && UM()->form()->errors['user_email'] == "You must provide your email" ){
            if( empty( UM()->form()->errors ) ){
                unset( UM()->form()->errors );
            }
            unset( UM()->form()->errors['user_email'] );
            
        }
    }

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    dear @champsupertramp thank for you reply
    after using this code it didn’t create the user
    but
    in stead of did the normal behavior of refreshing the page with leaving the inserted data in the field of the form as it with error message You are not allowed to add more users
    I found that
    page refreshed and form is cleared as if the registration is done

    so please advise
    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Please try this one. Let me know if you’re still encountering issues:

    add_action("um_submit_form_register","um_111121_email_field_optional", 1 );
    function um_111121_email_field_optional(){
        
        if( isset( UM()->form()->errors['user_email'] ) && UM()->form()->errors['user_email'] == "You must provide your email" ){
            
            unset( UM()->form()->errors['user_email'] );
            
            if( empty( UM()->form()->errors ) ){
                unset( UM()->form()->errors );
            }
        
        }
    }

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    dear @champsupertramp thank for you reply
    after using this code got extra issue
    1- it create the new user in stead of suspend user from create the new user
    2- in stead of did the normal behavior of refreshing the page with leaving the inserted data in the field of the form as it with error message You are not allowed to add more users
    I found that
    page refreshed and form is cleared as if the registration is done

    so please advise
    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Are you able to provide a video clip showing the issue? I couldn’t reproduce the issue on my end.

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    Dear @champsupertramp
    https://www.mediafire.com/file/860jn94syokxspe/video2218681415.mp4/file

    you will find in the video the first case of the registration is the right one and the second that has the issue and should be like the first one
    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Unfortuantely, I still couldn’t reproduce the issue on my end. Could you please provide the latest custom codes you have for this feature and I will try it on my end?

    Regards,

Viewing 15 replies - 16 through 30 (of 37 total)
  • The topic ‘control the number of registering new users’ is closed to new replies.