• Resolved scamrp1ckleballclub

    (@scamrp1ckleballclub)


    Unfortunately, I’m not a php developer. Have a tech background – windows development only. I’m a volunteer creating a membership site for a club and would like to set the WordPress ‘username’ field to a random value and not ask the user for it. I have found a function that will generate a random value for the field, but am unsure of how to set the username field in a code snippet, so that it is set before the new user is created in WordPress.

    Can this be done? If so, any help on the php code would be welcome.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    This is probably best done right on the login form via JavaScript, which maybe you’re more comfortable with anyway. Search the ‘net for “javascript auto fill form field” for suggestion on how to do so. The script itself is not specific to WP, but getting the script loaded on to the login page would be WP specific. To load an external .js file, enqueue it with wp_enqueue_script() from a callback added to the “login_enqueue_scripts”. That part of it is PHP. There are a number of examples at https://developer.wordpress.org/reference/functions/wp_enqueue_script/. Just be aware that the usual “wp_enqueue_scripts” action hook is not appropriate for login pages. Use “login_enqueue_scripts” instead.

    It sounds like your random name generator is for PHP. You’ll want one for JavaScript instead. There ought to be plenty of examples found through a search. If you don’t want users overriding the random name, you can make the field read-only with JavaScript.

    Your biggest challenge is having the script verify the chosen name is not already in use. You do need PHP for this. Your JavaScript (or jQuery) needs to make an Ajax request to the server to verify. Ajax in WP is a bit quirky. Read up about it here:
    https://developer.wordpress.org/plugins/javascript/ajax/

    If you would prefer to utilize PHP to randomly assign a name, you could have PHP determine a good random unused name from the start and pass it to the JavaScript by using wp_localize_script(). Might be easier than messing with Ajax.
    https://developer.wordpress.org/reference/functions/wp_localize_script/

    Apologies for a rather schizoid reply. There is more than one way to accomplish this and I’m not sure what would be best for you.

    Thread Starter scamrp1ckleballclub

    (@scamrp1ckleballclub)

    Thanks for your response. It gives me a number of options to look at. I’m starting to rethink my original request. I currently have the username automatically being set to the email address that the member enters on the registration form. At first, I didn’t like this approach, in case the user changes their email address. However, I need to ensure that each email address is unique (since we are interfacing with MailChimp for emails) and this approach does work initially, since usernames can’t be duplicated. I can then install a plug-in that allows me to change the username manually for anyone that changes their email address later. This will hardly happen and is only a maintenance issue every few months. Plus, there are only 200 members, so not an onerous task.

    Again, thanks for your input.

    Moderator bcworkz

    (@bcworkz)

    FWIW, WP will not allow duplicate emails as well, regardless of what you decide to do with usernames. It doesn’t really matter, so you don’t need to explain, but I’m curious. What is the objection to letting users pick their own username as is usual?

    Thread Starter scamrp1ckleballclub

    (@scamrp1ckleballclub)

    I didn’t realize that WP did not allow duplicate email addresses. I thought it was just usernames. So, I thought by setting the username to the email address, I was making sure that no duplicate emails were entered.

    I guess there is no reason for me to hide the username field on registration, if no duplicate email addresses can be entered. Thanks for the information again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Auto set Username for new users’ is closed to new replies.