Require First And Last Name During Registration

Note: This is part of the developer docs and is considered custom code.
Unfortunately, we cannot provide support for custom code at this time as we do not have the additional resources that would be necessary to provide support for custom code.

If you need assistance with this, please reach out to our list of consultants for further assistance:
https://codeable.io/developers/restrict-content-pro/

The registration form has fields for "First Name" and "Last Name" but they are not required. The following code will make these fields required and will display error messages if they're not filled out.

<?php
/** 
* Require first and last names during registration 
*  
* @param array $posted Array of information sent to the form. 
*  
* @return void 
*/
function ag_rcp_require_first_and_last_names( $posted ) {
if ( is_user_logged_in() ) {
return;
}

if ( empty( $posted['rcp_user_first'] ) ) {
rcp_errors()->add( 'first_name_required', __( 'Please enter your first name', 'rcp' ), 'register' );
}

if ( empty( $posted['rcp_user_last'] ) ) {
rcp_errors()->add( 'last_name_required', __( 'Please enter your last name', 'rcp' ), 'register' );
}
}

add_action( 'rcp_form_errors', 'ag_rcp_require_first_and_last_names' );

/** 
* Require first and last names during profile edit 
* 
* @param array $posted Array of information sent to the form. 
* @param int $user_id ID of the user editing their profile. 
* 
* @return void 
*/
function ag_rcp_require_first_and_last_name_profile_edit( $posted, $user_id ) {

if ( empty( $posted['rcp_first_name'] ) ) {
rcp_errors()->add( 'first_name_required', __( 'Please enter your first name', 'rcp' ) );
}

if ( empty( $posted['rcp_last_name'] ) ) {
rcp_errors()->add( 'last_name_required', __( 'Please enter your last name', 'rcp' ) );
}

}

add_action( 'rcp_edit_profile_form_errors','ag_rcp_require_first_and_last_name_profile_edit', 10, 2 );

Please note: All of these examples serve as a guideline and are here for your convenience. We do not provide support for troubleshooting or modifying these and we do not provide support for any custom development.  If you need help with any of these examples, consider hiring a developer.

Have more questions? Submit a request