Restrict Content Pro
Creating Custom Registration Fields
Coming soon: Restrict Content Pro is becoming Kadence Memberships. You may see both names used while we update the product, documentation, and customer experience.
Note: This content is provided as a resource within our developer documentation. Because custom code requires highly specific implementation, we encourage you to work with a qualified developer if you need guidance. You can find our recommended consultants here: https://codeable.io/developers/restrict-content-pro/
This tutorial will walk you through how to add custom fields to these pages:
- Membership registration form
[register_form] - Front-facing profile editor
[rcp_profile_editor] - Admin “Edit Customer” interface
All code should be added to either a custom plugin or your theme’s functions.php file. If you’re not familiar with creating a custom plugin, you can install Pluginception, use it to create a new plugin, paste in the code you want, customize it, and then activate it.
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 connects to the appropriate 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:
<?php
/**
* Adds the custom fields to the registration form and profile editor
*
*/
function pw_rcp_add_user_fields() {
$profession = get_user_meta( get_current_user_id(), 'rcp_profession', true );
$location = get_user_meta( get_current_user_id(), 'rcp_location', true );
?>
<p>
<label for="rcp_profession"><?php _e( 'Your Profession', 'rcp' ); ?></label>
<input name="rcp_profession" id="rcp_profession" type="text" value="<?php echo esc_attr( $profession ); ?>"/>
</p>
<p>
<label for="rcp_location"><?php _e( 'Your Location', 'rcp' ); ?></label>
<input name="rcp_location" id="rcp_location" type="text" value="<?php echo esc_attr( $location ); ?>"/>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'pw_rcp_add_user_fields' );
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_professionand/orrcp_locationto your own ID. Be sure to use underscores instead of spaces! This needs to be changed in theget_user_meta()function, the labelforattribute, the HTMLnameattribute on the input tab, and the HTMLidattribute. - Change the
$professionand$locationvariable names to better reflect the names of your input fields. Once again, use underscores instead of spaces. - Change the text associated with the labels ( ‘Your Profession’ and ‘Your Location’ ).
2. Adding The Edit Customer Screen Fields
Once we are finished, we want site admins to be able to edit the customer details, just like they can with name, email, etc. To do this, we need to add our new fields to the Edit Customer screen in Restrict Content Pro.
Adding the fields is nearly identical to adding them to the registration and profile forms. The only differences are the hook name we use and the HTML structure.
<?php
/**
* Adds the custom fields to the member edit screen
*
*/
function pw_rcp_add_member_edit_fields( $user_id = 0 ) {
$profession = get_user_meta( $user_id, 'rcp_profession', true );
$location = get_user_meta( $user_id, 'rcp_location', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_profession"><?php _e( 'Profession', 'rcp' ); ?></label>
</th>
<td>
<input name="rcp_profession" id="rcp_profession" type="text" value="<?php echo esc_attr( $profession ); ?>"/>
<p class="description"><?php _e( 'The member\'s profession', 'rcp' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_location"><?php _e( 'Location', 'rcp' ); ?></label>
</th>
<td>
<input name="rcp_location" id="rcp_location" type="text" value="<?php echo esc_attr( $location ); ?>"/>
<p class="description"><?php _e( 'The member\'s location', 'rcp' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'pw_rcp_add_member_edit_fields' );
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, the Profession and Location fields should appear on the Edit Customer 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.
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 Submit, Restrict Content Pro will display an error message.
To do this, we need to hook into the error-checking process of Restrict Content Pro (rcp_form_errors) and determine whether 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 matches a valid phone number.
<?php
/**
* Determines if there are problems with the registration data submitted
*
*/
function pw_rcp_validate_user_fields_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
if( empty( $posted['rcp_profession'] ) ) {
rcp_errors()->add( 'invalid_profession', __( 'Please enter your profession', 'rcp' ), 'register' );
}
if( empty( $posted['rcp_location'] ) ) {
rcp_errors()->add( 'invalid_location', __( 'Please enter your location', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'pw_rcp_validate_user_fields_on_register', 10 );
The empty() function does exactly what it sounds like: it checks whether 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 error 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.
4. Saving The User Fields On Registration
Once we have confirmed that the custom user fields contain the correct information, 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:
<?php
/**
* Stores the information submitted during registration
*
*/
function pw_rcp_save_user_fields_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_profession'] ) ) {
update_user_meta( $user_id, 'rcp_profession', sanitize_text_field( $posted['rcp_profession'] ) );
}
if ( ! empty( $posted['rcp_location'] ) ) {
update_user_meta( $user_id, 'rcp_location', sanitize_text_field( $posted['rcp_location'] ) );
}
}
add_action( 'rcp_form_processing', 'pw_rcp_save_user_fields_on_register', 10, 2 );
This will store the entered values for both fields in the usermeta database table, so we can retrieve them 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 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 the edit member form.
5. Saving The User Fields During Customer Edit and Profile Edit
This process is identical to how we save the fields during user registration, except that the hook and the parameters passed to it are slightly different. But everything else is exactly the same!
<?php
/**
* Stores the information submitted profile update
*
*/
function pw_rcp_save_user_fields_on_profile_save( $user_id ) {
if( ! empty( $_POST['rcp_profession'] ) ) {
update_user_meta( $user_id, 'rcp_profession', sanitize_text_field( $_POST['rcp_profession'] ) );
}
if( ! empty( $_POST['rcp_location'] ) ) {
update_user_meta( $user_id, 'rcp_location', sanitize_text_field( $_POST['rcp_location'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'pw_rcp_save_user_fields_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'pw_rcp_save_user_fields_on_profile_save', 10 );
As shown above, update the IDs passed into update_user_meta() if necessary. To allow customers to clear fields and save empty values, ensure the function handles empty inputs accordingly.
This completes the setup for custom registration fields, which can be edited directly by both site administrators and members in 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 them:
- Fields are primarily identified by their name and ID attributes. If you wish to change “profession” to “job”, search for
rcp_professionand replace it withrcp_job(or add a second set of fields and then update the names/ids). - Submitted field data 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 the functions. This applies to all functions shown in the tutorial.
You can view the complete example plugin here.
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 how the checkbox is displayed, 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:
<?php
/**
* Example for adding a custom checkbox field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom checkbox field to the registration form and profile editor.
*/
function ag_rcp_add_checkbox_field() {
$special_offers = get_user_meta( get_current_user_id(), 'rcp_special_offers', true );
?>
<p>
<input name="rcp_special_offers" id="rcp_special_offers" type="checkbox" value="1" <?php checked( $special_offers ); ?>/>
<label for="rcp_special_offers"><?php _e( 'Check to opt in to our special offers', 'rcp' ); ?></label>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_checkbox_field' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_checkbox_field' );
/**
* Adds the custom checkbox field to the member edit screen.
*/
function ag_rcp_add_checkbox_member_edit_fields( $user_id = 0 ) {
$special_offers = get_user_meta( $user_id, 'rcp_special_offers', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_special_offers"><?php _e( 'Special Offers', 'rcp' ); ?></label>
</th>
<td>
<input name="rcp_special_offers" id="rcp_special_offers" type="checkbox" <?php checked( $special_offers ); ?>/>
<span class="description"><?php _e( 'Check to opt in to our special offers', 'rcp' ); ?></span>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_checkbox_member_edit_fields' );
/**
* Determines if there are problems with the registration data submitted.
* Remove this code if you want the checkbox to be optional.
*/
function ag_rcp_validate_checkbox_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
if ( ! isset( $posted['rcp_special_offers'] ) ) {
rcp_errors()->add( 'invalid_special_offers', __( 'Please opt in to our special offers', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_checkbox_on_register', 10 );
/**
* Stores the information submitted during registration
*
* Sets the meta value to `true` to designate that the checkbox was checked on.
*/
function ag_rcp_save_checkbox_field_on_register( $posted, $user_id ) {
if ( isset( $posted['rcp_special_offers'] ) ) {
update_user_meta( $user_id, 'rcp_special_offers', true );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_checkbox_field_on_register', 10, 2 );
/**
* Stores the information submitted profile update
*
* Sets the meta value to `true` to designate that the checkbox was checked on.
*/
function ag_rcp_save_checkbox_field_on_profile_save( $user_id ) {
if ( isset( $_POST['rcp_special_offers'] ) ) {
// Set the user meta if the box was checked on.
update_user_meta( $user_id, 'rcp_special_offers', true );
} else {
// Delete the user meta if the box is unchecked.
delete_user_meta( $user_id, 'rcp_special_offers' );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_checkbox_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_checkbox_field_on_profile_save', 10 );
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.
With email fields, we can add extra validation to ensure the email is formatted as a proper email address. For that, we use the is_email() function.
<?php
/**
* Example for adding a custom email field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom email field to the registration form and profile editor.
*/
function ag_rcp_add_email_field() {
$alternate_email = get_user_meta( get_current_user_id(), 'rcp_alt_email', true );
?>
<p>
<label for="rcp_alt_email"><?php _e( 'Your Alternate Email Address', 'rcp' ); ?></label>
<input type="email" id="rcp_alt_email" name="rcp_alt_email" value="<?php echo esc_attr( $alternate_email ); ?>"/>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_email_field' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_email_field' );
/**
* Adds the custom email field to the member edit screen.
*/
function ag_rcp_add_email_member_edit_field( $user_id = 0 ) {
$alternate_email = get_user_meta( $user_id, 'rcp_alt_email', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_alt_email"><?php _e( 'Alternate Email', 'rcp' ); ?></label>
</th>
<td>
<input type="email" id="rcp_alt_email" name="rcp_alt_email" value="<?php echo esc_attr( $alternate_email ); ?>"/>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_email_member_edit_field' );
/**
* Determines if there are problems with the registration data submitted.
*/
function ag_rcp_validate_email_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
// Remove this segment if you don't want the email to be required.
if ( empty( $posted['rcp_alt_email'] ) ) {
rcp_errors()->add( 'missing_alt_email', __( 'Please enter an alternate email address', 'rcp' ), 'register' );
return;
}
// This throws an error if an email is entered but it's not a valid email address.
if ( ! empty( $posted['rcp_alt_email'] ) && ! is_email( $posted['rcp_alt_email'] ) ) {
rcp_errors()->add( 'invalid_alt_email', __( 'Please enter a valid alternate email address', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_email_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_email_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_alt_email'] ) ) {
update_user_meta( $user_id, 'rcp_alt_email', sanitize_email( $posted['rcp_alt_email'] ) );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_email_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_email_field_on_profile_save( $user_id ) {
if ( ! empty( $_POST['rcp_alt_email'] ) && is_email( $_POST['rcp_alt_email'] ) ) {
update_user_meta( $user_id, 'rcp_alt_email', sanitize_email( $_POST['rcp_alt_email'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_email_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_email_field_on_profile_save', 10 );
Radio
There are two main differences between this radio example and our walkthrough with text fields:
- The HTML display for the radio buttons.
- During validation, we also add an extra check to ensure the submitted value is one of our pre-approved options.
The HTML display for the radio buttons.
During validation, we also add an extra check to ensure the submitted value is one of our pre-approved options.
<?php
/**
* Example for adding custom radio fields to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom radio button fields to the registration form and profile editor.
*/
function ag_rcp_add_radio_fields() {
$referrer = get_user_meta( get_current_user_id(), 'rcp_referrer', true );
?>
<p>
<?php _e( 'How did you find out about our membership program?', 'rcp' ); ?>
</p>
<p>
<label for="rcp_referrer_friend">
<input name="rcp_referrer" id="rcp_referrer_friend" type="radio" value="friend" <?php checked( $referrer, 'friend' ); ?>/>
<?php _e( 'From a friend', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_search">
<input name="rcp_referrer" id="rcp_referrer_search" type="radio" value="search" <?php checked( $referrer, 'search' ); ?>/>
<?php _e( 'Search engine', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_social">
<input name="rcp_referrer" id="rcp_referrer_social" type="radio" value="social" <?php checked( $referrer, 'social' ); ?>/>
<?php _e( 'Social media', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_other">
<input name="rcp_referrer" id="rcp_referrer_other" type="radio" value="other" <?php checked( $referrer, 'other' ); ?>/>
<?php _e( 'Other', 'rcp' ); ?>
</label>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_radio_fields' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_radio_fields' );
/**
* Adds the custom radio button fields to the member edit screen.
*/
function ag_rcp_add_radio_member_edit_fields( $user_id = 0 ) {
$referrer = get_user_meta( $user_id, 'rcp_referrer', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_referrer"><?php _e( 'Referred By', 'rcp' ); ?></label>
</th>
<td>
<label for="rcp_referrer_friend">
<input name="rcp_referrer" id="rcp_referrer_friend" type="radio" value="friend" <?php checked( $referrer, 'friend' ); ?>/>
<?php _e( 'From a friend', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_search">
<input name="rcp_referrer" id="rcp_referrer_search" type="radio" value="search" <?php checked( $referrer, 'search' ); ?>/>
<?php _e( 'Search engine', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_social">
<input name="rcp_referrer" id="rcp_referrer_social" type="radio" value="social" <?php checked( $referrer, 'social' ); ?>/>
<?php _e( 'Social media', 'rcp' ); ?>
</label>
<br/>
<label for="rcp_referrer_other">
<input name="rcp_referrer" id="rcp_referrer_other" type="radio" value="other" <?php checked( $referrer, 'other' ); ?>/>
<?php _e( 'Other', 'rcp' ); ?>
</label>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_radio_member_edit_fields' );
/**
* Determines if there are problems with the registration data submitted.
* Remove this code if you want the radio button to be optional.
*/
function ag_rcp_validate_radio_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
// List all the available options that can be selected.
$available_choices = array(
'friend',
'search',
'social',
'other'
);
// Add an error message if the submitted option isn't one of our valid choices.
if ( ! in_array( $posted['rcp_referrer'], $available_choices ) ) {
rcp_errors()->add( 'invalid_referrer', __( 'Please select a valid referrer', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_radio_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_radio_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_referrer'] ) ) {
update_user_meta( $user_id, 'rcp_referrer', sanitize_text_field( $posted['rcp_referrer'] ) );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_radio_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_radio_field_on_profile_save( $user_id ) {
// List all the available options that can be selected.
$available_choices = array(
'friend',
'search',
'social',
'other'
);
if ( isset( $_POST['rcp_referrer'] ) && in_array( $_POST['rcp_referrer'], $available_choices ) ) {
update_user_meta( $user_id, 'rcp_referrer', sanitize_text_field( $_POST['rcp_referrer'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_radio_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_radio_field_on_profile_save', 10 );
Multi-check
Multi-check is similar to the checkbox example, but instead of a single checkbox, we have a field with several related checkboxes that are stored together. This is useful if you want customers to select multiple options within a set, such as a group of interests.
<?php
/**
* Example for adding a custom multicheck field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a multicheck field to the registration form an dprofile editor.
*/
function rcp_add_multicheck_field() {
$interests = get_user_meta( get_current_user_id(), 'rcp_interests', true );
if ( ! is_array( $interests ) ) {
$interests = array();
}
?>
<p>
<?php _e( 'Select your interests', 'rcp' ); ?>
</p>
<p>
<input name="rcp_interests[]" id="rcp_interests_art" type="checkbox" value="art" <?php checked( in_array( 'art', $interests ) ); ?>/>
<label for="rcp_interests_art"><?php _e( 'Art', 'rcp' ); ?></label> <br>
<input name="rcp_interests[]" id="rcp_interests_books" type="checkbox" value="books" <?php checked( in_array( 'books', $interests ) ); ?>/>
<label for="rcp_interests_books"><?php _e( 'Books', 'rcp' ); ?></label> <br>
<input name="rcp_interests[]" id="rcp_interests_sports" type="checkbox" value="sports" <?php checked( in_array( 'sports', $interests ) ); ?>/>
<label for="rcp_interests_sports"><?php _e( 'Sports', 'rcp' ); ?></label>
<!-- add more fields as desired -->
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'rcp_add_multicheck_field' );
add_action( 'rcp_profile_editor_after', 'rcp_add_multicheck_field' );
/**
* Adds the custom multicheck field to the member edit screen.
*/
function ag_rcp_add_multicheck_member_edit_fields( $user_id = 0 ) {
$interests = get_user_meta( $user_id, 'rcp_interests', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_interests"><?php _e( 'Interests', 'rcp' ); ?></label>
</th>
<td>
<input name="rcp_interests[]" id="rcp_interests_art" type="checkbox" value="art" <?php checked( in_array( 'art', $interests ) ); ?>/>
<span class="description"><?php _e( 'Art', 'rcp' ); ?></span> <br>
<input name="rcp_interests[]" id="rcp_interests_books" type="checkbox" value="books" <?php checked( in_array( 'books', $interests ) ); ?>/>
<span class="description"><?php _e( 'Books', 'rcp' ); ?></span> <br>
<input name="rcp_interests[]" id="rcp_interests_sports" type="checkbox" value="sports" <?php checked( in_array( 'sports', $interests ) ); ?>/>
<span class="description"><?php _e( 'Sports', 'rcp' ); ?></span>
<!-- add more fields as desired -->
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_multicheck_member_edit_fields' );
/**
* Shows an error message if none of the multicheck options are selected.
* Remove this code if you want the multicheck to be optional.
*/
function ag_rcp_validate_multicheck_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
if ( empty( $posted['rcp_interests'] ) || ! is_array( $posted['rcp_interests'] ) ) {
rcp_errors()->add( 'invalid_interests', __( 'Please select at least one interest', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_multicheck_on_register', 10 );
/**
* Stores the information submitted during registration
*
* Stores all the selected interests in an array.
*/
function ag_rcp_save_multicheck_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_interests'] ) && is_array( $posted['rcp_interests'] ) ) {
// First sanitize the array.
$interests = array_map( 'sanitize_text_field', $posted['rcp_interests'] );
// Now save.
update_user_meta( $user_id, 'rcp_interests', $interests );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_multicheck_field_on_register', 10, 2 );
/**
* Stores the information submitted profile update
*
* Stores all the selected interests in an array.
*/
function ag_rcp_save_multicheck_field_on_profile_save( $user_id ) {
if ( ! empty( $_POST['rcp_interests'] ) && is_array( $_POST['rcp_interests'] ) ) {
// First sanitize the array.
$interests = array_map( 'sanitize_text_field', $_POST['rcp_interests'] );
// Now save.
update_user_meta( $user_id, 'rcp_interests', $interests );
} else {
// Delete the user meta if no checkboxes are selected.
delete_user_meta( $user_id, 'rcp_interests' );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_multicheck_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_multicheck_field_on_profile_save', 10 );
The above example contains three checkboxes for interests: Art, Books, and Sports. A checkbox on its own looks like this:
<input name="rcp_interests[]" id="rcp_interests_art" type="checkbox" value="art" <?php checked( in_array( 'art', $interests ) ); ?>/>
<label for="rcp_interests_art"><?php _e( 'Art', 'rcp' ); ?></label>
When adding your own fields, you need to change the id, value, the value being compared in checked(), and the text itself in the label tag.
Select Drop Down
Except for the HTML markup, select dropdowns work almost exactly the same as radio buttons.
<?php
/**
* Example for adding a custom select field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom select field to the registration form and profile editor.
*/
function ag_rcp_add_select_field() {
$referrer = get_user_meta( get_current_user_id(), 'rcp_referrer', true );
?>
<p>
<label for="rcp_referrer"><?php _e( 'How did you find out about our membership program?', 'rcp' ); ?></label>
<select id="rcp_referrer" name="rcp_referrer">
<option value="friend" <?php selected( $referrer, 'friend'); ?>><?php _e( 'From a friend', 'rcp' ); ?></option>
<option value="search" <?php selected( $referrer, 'search'); ?>><?php _e( 'Search engine', 'rcp' ); ?></option>
<option value="social" <?php selected( $referrer, 'social'); ?>><?php _e( 'Social media', 'rcp' ); ?></option>
<option value="other" <?php selected( $referrer, 'other'); ?>><?php _e( 'Other', 'rcp' ); ?></option>
</select>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_select_field' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_select_field' );
/**
* Adds the custom select field to the member edit screen.
*/
function ag_rcp_add_select_member_edit_field( $user_id = 0 ) {
$referrer = get_user_meta( $user_id, 'rcp_referrer', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_referrer"><?php _e( 'Referred By', 'rcp' ); ?></label>
</th>
<td>
<select id="rcp_referrer" name="rcp_referrer">
<option value="friend" <?php selected( $referrer, 'friend'); ?>><?php _e( 'From a friend', 'rcp' ); ?></option>
<option value="search" <?php selected( $referrer, 'search'); ?>><?php _e( 'Search engine', 'rcp' ); ?></option>
<option value="social" <?php selected( $referrer, 'social'); ?>><?php _e( 'Social media', 'rcp' ); ?></option>
<option value="other" <?php selected( $referrer, 'other'); ?>><?php _e( 'Other', 'rcp' ); ?></option>
</select>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_select_member_edit_field' );
/**
* Determines if there are problems with the registration data submitted.
*/
function ag_rcp_validate_select_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
// List all the available options that can be selected.
$available_choices = array(
'friend',
'search',
'social',
'other'
);
// Add an error message if the submitted option isn't one of our valid choices.
if ( ! in_array( $posted['rcp_referrer'], $available_choices ) ) {
rcp_errors()->add( 'invalid_referrer', __( 'Please select a valid referrer', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_select_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_select_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_referrer'] ) ) {
update_user_meta( $user_id, 'rcp_referrer', sanitize_text_field( $posted['rcp_referrer'] ) );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_select_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_select_field_on_profile_save( $user_id ) {
// List all the available options that can be selected.
$available_choices = array(
'friend',
'search',
'social',
'other'
);
if ( isset( $_POST['rcp_referrer'] ) && in_array( $_POST['rcp_referrer'], $available_choices ) ) {
update_user_meta( $user_id, 'rcp_referrer', sanitize_text_field( $_POST['rcp_referrer'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_select_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_select_field_on_profile_save', 10 );
Textarea
In this example, we’re stripping all HTML from the textarea when saving it using the wp_filter_nohtml_kses() function. If you want to allow some HTML, consider using wp_kses() instead.
<?php
/**
* Example for adding a custom textarea field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom textarea field to the registration form and profile editor.
*/
function ag_rcp_add_textarea_field() {
$comments = get_user_meta( get_current_user_id(), 'rcp_customer_comments', true );
?>
<p>
<label for="rcp_customer_comments"><?php _e( 'Comments', 'rcp' ); ?></label>
<textarea id="rcp_customer_comments" name="rcp_customer_comments"><?php echo esc_textarea( $comments ); ?></textarea>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_textarea_field' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_textarea_field' );
/**
* Adds the custom textarea field to the member edit screen.
*/
function ag_rcp_add_textarea_member_edit_field( $user_id = 0 ) {
$comments = get_user_meta( $user_id, 'rcp_customer_comments', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_customer_comments"><?php _e( 'Comments', 'rcp' ); ?></label>
</th>
<td>
<textarea id="rcp_customer_comments" name="rcp_customer_comments" class="large-text" rows="10" cols="30"><?php echo esc_textarea( $comments ); ?></textarea>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_textarea_member_edit_field' );
/**
* Determines if there are problems with the registration data submitted.
* Remove this block if you want the textarea to be optional.
*/
function ag_rcp_validate_textarea_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
if ( empty( $posted['rcp_customer_comments'] ) ) {
rcp_errors()->add( 'invalid_customer_comments', __( 'Please enter a comment', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_textarea_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_textarea_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_customer_comments'] ) ) {
update_user_meta( $user_id, 'rcp_customer_comments', wp_filter_nohtml_kses( $posted['rcp_customer_comments'] ) );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_textarea_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_textarea_field_on_profile_save( $user_id ) {
if ( ! empty( $_POST['rcp_customer_comments'] ) ) {
update_user_meta( $user_id, 'rcp_customer_comments', wp_filter_nohtml_kses( $_POST['rcp_customer_comments'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_textarea_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_textarea_field_on_profile_save', 10 );
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.
<?php
/**
* Example for adding a custom URL field to the
* Restrict Content Pro registration form and profile editors.
*/
/**
* Adds a custom URL field to the registration form and profile editor.
*/
function ag_rcp_add_url_field() {
$website_url = get_user_meta( get_current_user_id(), 'rcp_website_url', true );
?>
<p>
<label for="rcp_website_url"><?php _e( 'Your Website URL', 'rcp' ); ?></label>
<input type="url" id="rcp_website_url" name="rcp_website_url" placeholder="https://" value="<?php echo esc_attr( $website_url ); ?>"/>
</p>
<?php
}
add_action( 'rcp_after_password_registration_field', 'ag_rcp_add_url_field' );
add_action( 'rcp_profile_editor_after', 'ag_rcp_add_url_field' );
/**
* Adds the custom URL field to the member edit screen.
*/
function ag_rcp_add_url_member_edit_field( $user_id = 0 ) {
$website_url = get_user_meta( $user_id, 'rcp_website_url', true );
?>
<tr valign="top">
<th scope="row" valign="top">
<label for="rcp_website_url"><?php _e( 'Website URL', 'rcp' ); ?></label>
</th>
<td>
<input type="url" id="rcp_website_url" name="rcp_website_url" placeholder="https://" value="<?php echo esc_attr( $website_url ); ?>"/>
</td>
</tr>
<?php
}
add_action( 'rcp_edit_member_after', 'ag_rcp_add_url_member_edit_field' );
/**
* Determines if there are problems with the registration data submitted.
* Remove this code if you want the URL field to be optional.
*/
function ag_rcp_validate_url_on_register( $posted ) {
if ( is_user_logged_in() ) {
return;
}
if ( empty( $posted['website_url'] ) ) {
rcp_errors()->add( 'invalid_website_url', __( 'Please enter a website URL', 'rcp' ), 'register' );
}
}
add_action( 'rcp_form_errors', 'ag_rcp_validate_url_on_register', 10 );
/**
* Stores the information submitted during registration.
*/
function ag_rcp_save_url_field_on_register( $posted, $user_id ) {
if ( ! empty( $posted['rcp_referrer'] ) ) {
update_user_meta( $user_id, 'rcp_referrer', esc_url_raw( $posted['rcp_referrer'] ) );
}
}
add_action( 'rcp_form_processing', 'ag_rcp_save_url_field_on_register', 10, 2 );
/**
* Stores the information submitted during profile update.
*/
function ag_rcp_save_url_field_on_profile_save( $user_id ) {
if ( ! empty( $_POST['rcp_referrer'] ) ) {
update_user_meta( $user_id, 'rcp_referrer', esc_url_raw( $_POST['rcp_referrer'] ) );
}
}
add_action( 'rcp_user_profile_updated', 'ag_rcp_save_url_field_on_profile_save', 10 );
add_action( 'rcp_edit_member', 'ag_rcp_save_url_field_on_profile_save', 10 );