sol2010 Posted January 19, 2021 Share Posted January 19, 2021 Hi all and especially @brian! I've searched the threads and can't find anyone else with the same request... though similar ones exit here and also here I am looking for a way to do this - put simply: have a code on the registration page that a user has to fill out before they can register (similar to a passcode) That way, registration will be by Invite Only and prevents all the scammers who sign up (we don't offer mass services) I am guessing the "Invitation Code" box could be added fine as a custom field ? But how to check that the entry is valid - ideally via Ajax prior to form submission? (e.g. against a passcode I have set up in the backend.?) If anyone has successfully achieved that, please advise! Thanks for help 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted January 19, 2021 Share Posted January 19, 2021 11 hours ago, sol2010 said: I am guessing the "Invitation Code" box could be added fine as a custom field ? yes. ✅ 11 hours ago, sol2010 said: But how to check that the entry is valid - ideally via Ajax prior to form submission? (e.g. against a passcode I have set up in the backend.?) if you wanted to do this without any coding (e.g hook), then I would put the validation within the custom field as regex... that would add the field to the registration page (and also checkout in the cart), and unless the invite code is entered correctly, e.g 'brian' (case sensitive - so 'Brian' woudn't be valid). an error message will be shown. if you wanted to allow multiple different invite codes to be valid, then you can add a list of valid options to the regex.. /^(tom|dick|harry)$/ or if you wanted to make it case-insensitive, e.g so haRrY would be valid, then it would be... /^(tom|dick|harry)$/i ... though you probably shouldn't need to use that as you likely want what is entered to be exact as the valid code(s). I think there is only one downside to this solution - but that would only apply if this "Invite Code" custom field was added after existing clients had already registered - they would be unable to edit their profile because the IC field wouldn't have been set for them, but it's required and they won't know what the valid code is/are.... the hook below should fix by injecting the code into the field if it doesn't exist - additionally, it converts the text field to a password to hide the code (though it could be viewed from the browser source code if they want to). <?php # Inject Invite Code Into Client Profiles Hook # Written by brian! function client_profiles_invite_code_hook($vars) { $customfields = $vars['customfields']; $invitefield = "Invite Code"; $invitecode = "123456789012345"; foreach ($customfields as $key => $field) { if ($field['name'] == $invitefield && empty($field['value'])) { $customfields[$key]['input'] = str_replace("text","password",$field['input']); $customfields[$key]['input'] = str_replace('value=""','value="'.$invitecode.'"',$customfields[$key]['input']); } } return array("customfields" => $customfields); } add_hook("ClientAreaPageProfile", 1, "client_profiles_invite_code_hook"); the only two lines you should need to change is the $invitefield and $invitecode variables - the first is the name of your custom field; the second is a valid invite code value. there could be a further issue if you changed the invite code after a user had registered - because they would have an existing value that had become invalid and would then be unable to edit their profile.... that's fixable though, as would be pulling the valid values from the database if you really had to - but i'm trying to keep this simple for now until you get your head around how this might work. 1 Quote Link to comment Share on other sites More sharing options...
sol2010 Posted January 20, 2021 Author Share Posted January 20, 2021 You beauty @brian! Registration page code works a treat (after a bit of customising.) One query so far: how can I check in the hook, if a client is active (I don't want this invite code showing in any inactive client accounts?) Thank you @brian! I would love to buy you a beer or coffee so do tell me how that is feasible. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted January 20, 2021 Share Posted January 20, 2021 14 hours ago, sol2010 said: Registration page code works a treat (after a bit of customising.) you meant the invite code is no longer "brian" 🤪 14 hours ago, sol2010 said: One query so far: how can I check in the hook, if a client is active (I don't want this invite code showing in any inactive client accounts?) you would just need two more lines of code and to wrap the function if a conditional if statement... <?php # Inject Invite Code Into Client Profiles Hook # Written by brian! function client_profiles_invite_code_hook($vars) { $client = Menu::context('client'); $status = $client->status; if ($status == "Active") { $customfields = $vars['customfields']; $invitefield = "Invite Code"; $invitecode = "123456789012345"; foreach ($customfields as $key => $field) { if ($field['name'] == $invitefield && empty($field['value'])) { $customfields[$key]['input'] = str_replace("text","password",$field['input']); $customfields[$key]['input'] = str_replace('value=""','value="'.$invitecode.'"',$customfields[$key]['input']); } } return array("customfields" => $customfields); } } add_hook("ClientAreaPageProfile", 1, "client_profiles_invite_code_hook"); 1 Quote Link to comment Share on other sites More sharing options...
sol2010 Posted January 20, 2021 Author Share Posted January 20, 2021 Thank you once again @brian!, very helpful. I'm not sure where you are located, but in any case, I'm sending you some virtual sunshine, pizza and surf, to be consumed in any order, though perhaps not at the same time 😉 😎🌞🏄♂️🍕 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted January 21, 2021 Share Posted January 21, 2021 17 hours ago, sol2010 said: Thank you once again @brian!, very helpful. I'm not sure where you are located, but in any case, I'm sending you some virtual sunshine, pizza and surf, to be consumed in any order, though perhaps not at the same time 😉 😎🌞🏄♂️🍕 send umbrella or canoe - the rain won't stop! ☔🛶 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.