• Hello Dear @champsupertramp
    as per previous conversation in other post of controling number of new registered users that logged in user can add
    but please put in consideration that will use the snippets that provided in the previous post

    need to add field (new users limit) in edit user profile page in back end or even in UM profile page
    this field I can add in it number of users the current user can register while he is logged in using UM register form
    so if I add for specific user in this field number 4 that mean he can add only 4 new users and then he will not be able to add more and if I change this number to be 20 he will get the ability again until he reach 20 registered new user then he will be not able to add more

    best regards

Viewing 15 replies - 1 through 15 (of 46 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    You can try this code snippet:

    
    // Enable Register form for logged-in users
    add_filter( 'um_registration_for_loggedin_users', '__return_true' );
    
    // 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 ){
      
            $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => get_current_user_id(),
                    "fields" => "ids"
                    )
                )
            );
    	
    	    if( UM()->form()->errors['user_email'] === "You must provide your email" ){
               unset( UM()->form()->errors['user_email'] );
    		}
    
              $user_limit = (int)um_user("registration_limit");
              if( $users > $user_limit ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           
    
            if( empty( UM()->form()->errors ) ){
                unset( UM()->form()->errors );
            }
    
    }

    You need to add a number field in the Profile form with the meta key registration_limit to make the above code work.

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Dear @champsupertramp thank you very much I tested it and it work
    would you please help in this
    to add field also in profile page count how many number of users the current user already registered

    best regards

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    also bro @champsupertramp if that possible please help
    to add button in current user profile only admin can find it and press it to reset the number of registered users by this current user to be 0 or non
    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Here’s a code snippet to create a new shortcode to display the total number of registered users of a user:

    add_shortcode("um_display_total_referral", "um_112521_display_total_referral");
    function um_112521_display_total_referral(){
             $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => um_user("ID"),
                    "fields" => "ids"
                    )
                )
            );
    
            return count( $users );
    }

    Now, add this shortcode to the Profile Form [um_display_total_referral] using the Shortcode field in the UM Form Builder.

    Regards,

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Here’s a shortcode to display the reset button [um_reset_referral]. Add the shortcode to the Profile Form using the Shortcode field via UM Form Builder.

    add_shortcode("um_reset_referral","um_112521_reset_referral_button");
    function um_112521_reset_referral_button(){
    
        return "<a href='?um_reset_referral=".um_user("ID")."' class='um-button'>Reset Referral</a>";
    }
    
    add_action("template_redirect","um_112521_reset_referral");
    function um_112521_reset_referral(){
      
        if( isset( $_REQUEST['um_reset_referral'] ) && current_user_can("manage_options") ){
            global $wpdb;
            $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->usermeta} WHERE meta_key = 'um_user_referral' AND meta_value = %d ", $_REQUEST['um_reset_referral'] ) );
        }
    }

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Dear @champsupertramp thank you for the code but I got the next

    1- for the first code of showing number of registered user by the current user it is work right

    but for the second code I got this errors
    2- fro field that count how many number of users the current user already registered it appear for all users = 1
    3- for me as admin its appear like this registration limit = 3 and user that already registered = 1 (this is not right)
    then I tried to add new user and I got the message of not allowing to add new users

    4- as I told current registered users number all the time = 1 so the reset button function not working
    5- reset button appear to all users in their profile and the right thing that only admin can view this button in their profile to do this adjusting him self not then

    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Please try this one to fix the total referral number:

    add_shortcode("um_display_total_referral", "um_112521_display_total_referral");
    function um_112521_display_total_referral(){
             $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => um_user("ID"),
                    "fields" => "ids"
                    )
                )
            );
    
            return $users;
    }

    >>> 5- reset button appear to all users in their profile and the right thing that only admin can view this button in their profile to do this adjusting him self not then
    ** You can change the Shortcode field privacy in the Form Builder. Please see the screenshot: https://drive.google.com/file/d/14TwDOfo_m_RpKX9VNxoEHJgYxfCsMulC/view?usp=sharing

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Hi @champsupertramp
    after using the new code I got the next
    1- registration limit stopped and current user became able to add unlimited users
    2- field that count how many number of users the current user already registered it appear = 0 without any change even after add new users
    3- as result reset button didn’t give any effect

    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Could you please share your existing codes here so we can review them? Use the CODE in the text editor to add the code format in your reply.

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    dear I used the codes you gave but I found where is the problem
    the next code for create email if no email in UM register form to avoid error in registration

    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 );
            }
        
        }
    }

    when I disabled this snippets every thing work well
    so please advise
    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Please replace the above code in your recent reply with this:

    add_action("um_submit_form_register","um_110821_validate_registration_limit", 1 );
    function um_110821_validate_registration_limit( $post_form ){
      
            $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => get_current_user_id(),
                    "fields" => "ids"
                    )
                )
            );
    	
    	    if( UM()->form()->errors['user_email'] === "You must provide your email" ){
               unset( UM()->form()->errors['user_email'] );
    		}
    
             $user_limit = (int)um_user("registration_limit");
              if( $users > $user_limit ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           
            if( empty( UM()->form()->errors ) ){
                unset( UM()->form()->errors );
            }
    
    }

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Hi @champsupertramp
    same issue that happened because of the code of adding email in case not adding email while registration

    after using the new code I got the next
    1- registration limit stopped and current user became able to add unlimited users
    2- field that count how many number of users the current user already registered it appear = 0 without any change even after add new users
    3- as result reset button didn’t give any effect

    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Can you please share all the custom codes so I can test them on my end?

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Dear @champsupertramp also there are note want to let you know about it
    after doing the reset the counting and limitation stopped and it works only if I did log out and log in again
    regards

Viewing 15 replies - 1 through 15 (of 46 total)
  • The topic ‘control numbers of new users’ is closed to new replies.