It is very common for users of Restrict Content Pro to want to add custom fields to the registration and profile forms so that members can enter and store additional information, perhaps for their location, profession, or personal interests (or anything else). I have shown how to add these kind of fields before, but the method is a bit out of date and not fully developed, so I’d like to walk through the process again so that you can easily add new fields to the registration form, the profile editor, and also the member edit screen in the admin section of Restrict Content Pro.
We will be adding the fields shown in the images below:
Step 1 – Add fields to the registration and profile forms
Adding fields to the registration and profile forms is simply a matter of writing a function that outputs the HTML for the fields and is connected to the proper actions. For the registration form, we need to use the rcp_after_password_registration_field action, and for the profile form we need to use the rcp_profile_editor_after action. Note, there are other action hooks you may use but these are the most likely ones you will want.
Our function is very simple:
We have simply setup the HTML and used get_user_meta() to retrieve the saved information for the currently logged-in user. If you put this code into your theme or an active plugin, the fields should now show up on the registration and profile forms. They won’t save any information yet, but they will show up.
Step 2 – Adding the Edit Member screen fields
Once we are finished, we want site admins to be able to edit the member details, just like they are able default user fields (name, email, etc). To do this, we need to add our new fields to the Edit Member screen in Restrict Content Pro. Adding the fields is nearly identical to adding them to the registration and profile forms. The only difference is the hook name we use and the structure of the HTML.
You should now have the Profession and Location fields showing up on Edit Member screen. Now it’s time to process and save the data during form submission.
Step 3 – Checking for errors during registration
In the case that we want to make our fields required (so they cannot be left blank during registration), we need to hook into the error checking process of Restrict Content Pro and determine if our custom fields validate. To do that, we can use the rcp_form_errors hook to determine if our fields have valid data entered in them. For this example, we’re simply going to assume that Profession and Location both need to be filled out, but we don’t really care what kind of information is entered in them. If you are adding a phone number field, for example, you would probably want to ensure the value entered matched a valid phone number.
The empty() function does exactly what it sounds like: it checks to see if the specified field is empty. If either field is empty, we call rcp_errors()->add() in order to register a new registration error. Restrict Content Pro will not allow the registration form to be submitted until all errors checks pass.
Step 4 – Saving the user fields on registration
Once we have confirmed that the custom user fields have proper information entered in them, we need to save the data to the member’s user meta. To do this, we use the rcp_form_processing hook and the update_user_meta() function:
This will store the entered value for both fields into the usermeta database so that we can retrieve it at anytime. Now it’s time to save data when submitting the profile form and also the edit member form.
Step 5 – Saving the user fields during member edit and profile edit
This process is identical to how we save the fields during user registration except the hook and the parameters passed to the hook are a little different, but everything else is exactly the same.
And that’s it! We now have fully functional custom registration fields that can also be edited by both site admins and member’s themselves from the profile editor.
Going further and adding more or different fields
All of the code provided in this tutorial is meant as an example. Since it is unlikely that these two fields will fit your exact needs, here are a few tips for how to modify it to fit your needs:
Fields are primarily identified by their name and id attributes. If you wish to change “profession” to “job”, search for “rcp_profession” and replace it with “rcp_job” (or add a second set of fields and then update the names/ids).
Field data submitted should be validated to ensure it matches the expected and desired format.
When adding additional fields, do not duplicate functions, instead duplicate existing code inside of the functions. This applies to all functions shown in this tutorial.
If you have problems, ask for help, I’m more than happy to assist!