Remove the username field and allow email only registration

By default, the Restrict Content Pro registration form has two separate fields:

  1. Username
  2. Email

This tutorial will show you how to remove the username field and allow registration with email only. Members will be able to login with their email address directly instead of remembering a separate username.

Step 1: remove the username form field

This can be done in one of two ways. You only need to choose one:

Hide the field with CSS

This option leaves the markup on the page, but hides it with CSS. Add this custom CSS snippet:

#rcp_user_login_wrap {
    display: none;
}

Or, remove the HTML completely

This option removes the entire username markup from the page. You need to override and edit two template files:

  1. register.php
  2. register-single.php

If you're not sure how to do this, read  our tutorial on editing template files.

In both files, the following piece of HTML needs to be deleted:

<p id="rcp_user_login_wrap">
    <label for="rcp_user_login"><?php echo apply_filters ( 'rcp_registration_username_label', __( 'Username', 'rcp' ) ); ?></label>
    <input name="rcp_user_login" id="rcp_user_login" class="required" type="text" <?php if( isset( $_POST['rcp_user_login'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_user_login'] ) . '"'; } ?>/>
</p>

Once you've finished one of these steps, the username text field should be completely hidden from the page.

Step 2: set the email as the username

Just removing the field isn't enough, because RCP is expecting a username to be entered and will throw an error if it's not filled out. To combat this, we need to remove the "Username is empty" error message and assign the email as the username. To do that, add this code to your child theme's functions.php file or to a custom plugin:

<?php
/**
 * This will remove the username requirement on the registration form
 * and use the email address as the username.
 */
function jp_rcp_user_registration_data( $user ) {
	rcp_errors()->remove( 'username_empty' );
	$user['login'] = $user['email'];
	return $user;
}

add_filter( 'rcp_user_registration_data', 'jp_rcp_user_registration_data' );