• 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 - 1 through 15 (of 37 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    You can try this code snippet:

    
    // 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");
    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( $users > 3 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
            }
        }  
    
    }

    The above code will add a referer user id to the newly registered user. When the currently logged-in user has reached three user registrations, it will display an error “You are not allowed to add more users” in the Email address field of the form.

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    thank you for helping
    but unfortunately I tested and I got the next result
    yes user after adding the third user get not allowed message but at the same time when I went back to users page in backend I found that the 4th user has been created

    so please advise regarding this issue how to stop this creation
    best regards

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

    (@champsupertramp)

    Hi @makmerghen

    Try changing it to 2.

    if( $users > 2 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
    }

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    actually that what I did in my first test I change 3 to be 2 to test it fast
    but it gave me the previous result
    regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Try changing the priority to 1.

    add_action("um_submit_form_register","um_110821_validate_registration_limit", 1 );

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    I did that and it didn’t create the user
    but when I tried to increase the limit of number of registration by change 2 in
    if( $users > 2 ){
    UM()->form()->add_error(‘user_email’, __( ‘You are not allowed to add more users’, ‘ultimate-member’ ) );
    }
    to be as example 10 I got error

    array (
      'message' => 'Cannot create a user with an empty login name.',
      'dump' => 
      array (
        'ID' => 50,
        'display_name' => ' ',
      ),
      'changes' => 
      array (
        'form_id' => '944',
        'custom_fields' => 'a:4:{s:10:"user_email";a:15:{s:6:"in_row";s:9:"_um_row_1";s:10:"in_sub_row";s:1:"0";s:9:"in_column";i:1;s:8:"in_group";s:0:"";s:4:"type";s:4:"text";s:7:"metakey";s:10:"user_email";s:8:"position";i:1;s:5:"title";s:14:"E-mail Address";s:9:"min_chars";i:0;s:10:"visibility";s:3:"all";s:5:"label";s:14:"E-mail Address";s:6:"public";s:1:"1";s:8:"validate";s:12:"unique_email";s:9:"max_chars";i:0;s:8:"editable";b:1;}s:10:"first_name";a:12:{s:5:"title";s:10:"First Name";s:7:"metakey";s:10:"first_name";s:4:"type";s:4:"text";s:5:"label";s:10:"First Name";s:8:"required";i:0;s:6:"public";i:1;s:8:"editable";i:1;s:8:"position";i:2;s:6:"in_row";s:9:"_um_row_1";s:10:"in_sub_row";s:1:"0";s:9:"in_column";i:2;s:8:"in_group";s:0:"";}s:9:"last_name";a:12:{s:5:"title";s:9:"Last Name";s:7:"metakey";s:9:"last_name";s:4:"type";s:4:"text";s:5:"label";s:9:"Last Name";s:8:"required";i:0;s:6:"public";i:1;s:8:"editable";i:1;s:8:"position";i:3;s:6:"in_row";s:9:"_um_row_1";s:10:"in_sub_row";s:1:"0";s:9:"in_column";i:2;s:8:"in_group";s:0:"";}s:9:"_um_row_1";a:5:{s:4:"type";s:3:"row";s:2:"id";s:9:"_um_row_1";s:8:"sub_rows";i:1;s:4:"cols";i:2;s:6:"origin";s:9:"_um_row_1";}}',
        'mode' => 'register',
        'core' => 'register',
        'use_custom_settings' => '1',
        'role' => 'customer',
        'template' => 'register',
        'max_width' => '600',
        'icons' => 'label',
        'primary_btn_word' => 'Register',
        'secondary_btn' => '1',
        'secondary_btn_word' => 'Login',
        'use_gdpr' => '',
        'use_gdpr_content_id' => '0',
        'use_gdpr_toggle_show' => 'Show privacy policy',
        'use_gdpr_toggle_hide' => 'Hide privacy policy',
        'use_gdpr_agreement' => 'Please confirm that you agree to our privacy policy',
        'use_gdpr_error_text' => 'Please confirm your acceptance of our privacy policy',
        'g_recaptcha_status' => '1',
        'use_terms_conditions' => '1',
        'use_terms_conditions_content_id' => '1259',
        'use_terms_conditions_toggle_show' => 'Show Terms',
        'use_terms_conditions_toggle_hide' => 'Hide Terms',
        'use_terms_conditions_agreement' => 'Checkbox agreement description',
        'use_terms_conditions_error_text' => 'You must agree to our terms & conditions',
        'show_social' => '1',
        'show_social_2steps' => '1',
        '1step_link_matched_email' => '1',
        'show_flash_screen' => '1',
        'g_recaptcha_score' => '0',
        'code' => '',
        'csrf' => '',
        'dig_nounce' => 'b7df63b26b',
        'digit_ac_otp' => '',
        'um_request' => '',
        '_wpnonce' => 'a0fd5f8eb5',
        '_wp_http_referer' => '/en/register/',
        'user_email' => '[email protected]',
        'first_name' => '',
        'last_name' => '',
        'submitted' => 
        array (
          'code' => '',
          'csrf' => '',
          'dig_nounce' => 'b7df63b26b',
          'digit_ac_otp' => '',
          'form_id' => '944',
          'um_request' => '',
          '_wpnonce' => 'a0fd5f8eb5',
          '_wp_http_referer' => '/en/register/',
          'user_email' => '[email protected]',
          'first_name' => '',
          'last_name' => '',
          'user_login' => '[email protected]',
          'user_password' => 'TRWDaQTU',
          'timestamp' => 1636476607,
        ),
        'timestamp' => 1636476607,
        'user_login' => '[email protected]',
        'user_password' => 'TRWDaQTU',
      ),
    )
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    UM doesn’t have this text “Cannot create a user with an empty login name”. Does this text “You are not allowed to add more users” show in the User Email field when it reached the max registered users by a user?

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    Does this text “You are not allowed to add more users” show in the User Email field when it reached the max registered users by a user?

    yes

    my question is
    in the provided code you gave 2 numbers
    add_action(“um_submit_form_register”,”um_110821_validate_registration_limit”, 1 );

    and
    if( $users > 2 ){
    UM()->form()->add_error(‘user_email’, __( ‘You are not allowed to add more users’, ‘ultimate-member’ ) );
    }

    which one should I change to control the number of allowed registered users by one logged in user
    regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    You can change the number 2 to your preferred limit.

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    dear regarding your message
    UM doesn’t have this text “Cannot create a user with an empty login name
    since I am using plugin do integration with UM to add mobile number with otp and to create username from the field of mobile number
    do you think this message related to this plugin
    regards

    Thread Starter makmerghen

    (@makmerghen)

    also I did the next I disabled the otp plugin and I used the snippets you gave but it didn’t worked it and I added unlimited users not 3 only
    so please advise
    this is the code I used

    // 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( $users > 3 ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
            }
        }  
    
    }
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    Do you have the email address field in the Register form?

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    thank you dear for your message
    you were right I was remove email field and after I add it again the issue was solved

    now limitation is for all kind of users
    can we make it more specific
    as example
    users who has membership30 role has 30 limits
    users who has membership50 role has 50 limits
    users who has membership100 role has 100 limits
    users who has disabled_membership roles has 0 limits
    and other roles has no limit

    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @makmerghen

    You can try the following:

    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,

    Thread Starter makmerghen

    (@makmerghen)

    thanks a lot for the code but unfortunately I got error in line 23

    this is the all code I used

    // 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
           }
        }  
    
    }

    best regards

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