Divi WordPress Theme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix Reply To: Force/show nickname field during user registration

Discussion in 'Misc WordPress Requests' started by bcworkz, Oct 25, 2017.

  1. bcworkz


    Reply To: Force/show nickname field during user registration, by bcworkz

    It took me a while, but I understand now, thanks for clarifying. My last suggestion accomplishes what you want. I’m sorry it’s difficult to follow, I threw a lot of terse information at you. Lets ignore the validation stuff for now and focus on the parts needed to get the basic functionality. Validation is important for security and usability, but it can come later.

    There are two parts that need to be coded. Most people create a child theme to contain all of their custom code, but you could write a custom plugin instead. Either way works, the actual code is the same. First you need to add the input field to the default new user screen. This is done from a callback function added to the ‘user_new_form’ action. More on hooking actions in general is available in the Plugin Handbook. Your callback just needs to echo out a normal label and HTML text input field that includes a unique name attribute.

    It’s often helpful to see where action hooks are applied when coding callbacks. In this case it’s in user-new.php. You can see your output will appear just before the submit button.

    The second part is processing the new user form submittal and saving the entered value somewhere. To do that, hook the ‘register_new_user’ action. Not the ‘register_user’ action I mentioned in my last post, sorry for any confusion. The submitted value can be taken from the $_POST superglobal array, keyed as the name attribute you assigned to your input field. Nicknames are saved in user meta, keyed as “nickname”. The ‘register_new_user’ action is applied in the register_new_user() function, which WP calls while processing the form submittal.

    Once you get the basic functionality working, you can then address the validation and sanitation needs. At a minimum, use sanitize_text_field() before saving data in the DB. Ideally, your code should ensure the submitted nickname is not already used by querying the usermeta table for other instances of the submitted value.

    Reply To: Force/show nickname field during user registration

Share This Page

Monarch Social Sharing Plugin