control numbers of new users
-
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 postneed 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 morebest regards
-
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,
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 registeredbest regards
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 regardsHere’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,
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,
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 users4- 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 thenbest regards
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=sharingRegards,
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 effectbest regards
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,
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 registrationadd_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 regardsPlease 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,
Hi @champsupertramp
same issue that happened because of the code of adding email in case not adding email while registrationafter 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 effectbest regards
@champsupertramp HERE IS THE CODE SNIPPETS OF ALL CODES https://www.mediafire.com/file/qn1m3m1eopdx6xq/code-snippets.json/file
BEST REGARDDear @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
- The topic ‘control numbers of new users’ is closed to new replies.