Creating Custom Registration Fields

This tutorial will walk you through how to add custom fields to these pages:

All code should be added to either a custom plugin or your theme's functions.php file.

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. There are other action hooks you may use instead if you want to change the positioning, but these are the most likely ones you will want.

Our function is very simple:

This code is adding two extra fields: one text field for "Your Profession", and another text field for "Your Location". We use  get_user_meta() to retrieve the saved information for the currently logged in user.

When you put this code into your theme's functions.php file or a custom 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!

If you don't want the fields to show up on the profile editor, simply remove this line:

add_action( 'rcp_profile_editor_after', 'pw_rcp_add_user_fields' );

Want to change the fields to something else? Here's a list of what you need to edit:

  • Change all instances of rcp_profession and/or rcp_location to your own ID. Be sure to use underscores instead of spaces! This needs to be changed in the get_user_meta() function, the label 'for' attribute, the HTML 'name' attribute on the input tab, and the HTML 'id' attribute.
  • Change the $profession and $location variable names to better reflect the name of your input fields. Once again, use underscores instead of spaces.
  • Change the text associated with the labels ( 'Your Profession' and 'Your Location' ).

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 can with 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.

We're using get_user_meta() in the same way and using the same input fields. The surrounding HTML is just slightly different to use table markup.

Once this code has been added, you should now have the Profession and Location fields showing up on the Edit Member screen.

Changing these fields to something else? You'll need to edit all the same IDs and variables as before. They should match in both functions.

Now it's time to process and save the data during form submission.

Step 3 - Checking for errors during registration

This is an optional step in case you want to make your fields required. If they're left blank when the user clicks to submit their registration, Restrict Content Pro will show an error message.

To do this, we need to hook into the error checking process of Restrict Content Pro ( rcp_form_errors ) and determine if our fields validate. In our 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 might 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 create a new registration error. Restrict Content Pro will not allow the registration form to be submitted until all errors checks pass.

If you want to make your fields required, be sure to update the rcp_profession and rcp_location IDs again, as well as the error messages. If you want your fields to be optional, skip this step completely.

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 table so we can retrieve it at any time.

If you've changed the rcp_profession and/or rcp_location field IDs in previous steps, you'll need to update them again here! The IDs entered into update_user_meta() here need to be exactly the same as what you set them to in get_user_meta() in steps 1 and 2.

Now it's time to save data when submitting the profile form and 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 into the hook are a little different. But everything else is exactly the same!

Just like above, don't forget to update the IDs passed into update_user_meta() if necessary.

And that's it! We now have fully functional custom registration fields that can also be edited by both site admins and members themselves from the profile editor.

Tips for modifying the code

All of the code provided in this tutorial is meant as an example. Since it's unlikely that these two fields will fit your exact needs, here are a few tips for how to modify it:

  • 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 show in the tutorial.

You can view the  complete example plugin here or you can download it as a zip file that is ready to be uploaded to your WordPress site as a plugin.

Examples using other field types

Our main example above uses a standard text input field. Below we have a few examples using other field types.

Checkbox

If you want to add a checkbox field, each part of the process will need to be adjusted, including the checkbox display, how you check for errors, and how you save that data.

Here's a complete example with the code for each step of the process:

To edit the above checkbox example, all you need to do is change the rcp_special_offers ID, the $special_offers variable, and the various labels/descriptions. The checkbox value doesn't need to be edited, since it just has on/off states.

Email

With email fields, we can add some extra validation to make sure the email is formatted like a proper email address. For that we use the is_email() function.

Number

In this example, we sanitize the number with absint() which ensures the number is a positive integer. If you want to allow for negative numbers as well, use intval() instead.

Radio

There are two main differences between this radio example and our walkthrough with text fields:

  1. The HTML display for the radio buttons.
  2. During validation, we also add an extra check to make sure the submitted value is only one of our pre-approved options.

Select drop down

Except for the HTML markup, select drop downs work almost exactly the same as radio buttons.

Textarea

In this example, we're stripping all HTML out of the textarea when saving it with the wp_filter_nohtml_kses() function. If you want to allow some HTML, consider using wp_kses() instead.

URL

A URL field is a specialized type of text field so much of the process is the same, but we can sanitize the input differently using the esc_url_raw() function.