Here, you will find some custom code snippets for various functions that you won’t get on the available plugin settings. This list will keep on updating as we move forward.
Wanna know how to add these custom code snippet?
User Roles #
Give multiple user roles #
As the name suggests, this code will give two roles to the users. It will make the user an admin and an editor. You can add or remove roles from the code as per your need.
add_filter( 'user_registration_after_register_user_action', 'ur_add_user_role', 10, 3 );
function ur_add_user_role( $form_data, $form_id, $user_id ) {
if ( in_array( $form_id, array( 40 ), true ) ) {
$user = new WP_User( $user_id );
$new_role = array( 'administrator', 'editor' ); // New role to assign to user.
foreach ( $new_role as $role ) {
$user->add_role( $role );
}
}
}
Username Validations #
For username to be the entered email #
This code will set the email address you have entered as the username for the respective user.
add_action( 'user_registration_after_register_user_action', 'ur_insert_username', 1, 3 );
function ur_insert_username( $valid_form_data, $form_id, $user_id ) {
$user_login = $valid_form_data['user_email']->value;
global $wpdb;
$wpdb->update( $wpdb->users, array( 'user_login' => $user_login ), array( 'ID' => $user_id ) );
}
add_filter( 'user_registration_before_register_user_filter', 'ur_set_email_as_username', 10, 2 );
function ur_set_email_as_username( $valid_form_data, $form_id ) {
$valid_form_data['user_login'] = $valid_form_data['user_email'];
return $valid_form_data;
}
Show First Name and Last Name as the username #
This code will concat the first and last name and use it as the username.
add_action( 'user_registration_after_register_user_action', 'ur_insert_username', 1, 3 );
function ur_insert_username( $valid_form_data, $form_id, $user_id ) {
global $wpdb;
$firstname = isset( $valid_form_data['first_name'] ) ? $valid_form_data['first_name']->value : '';
$lastname = isset( $valid_form_data['last_name'] ) ? $valid_form_data['last_name']->value : '';
$custom_username = $firstname . "-" . $lastname;
$user = get_user_by('login', $custom_username);
if( ! empty( $user ) ) {
$custom_username = $custom_username ."-" . $user_id;
}
$wpdb->update(
$wpdb->users,
['user_login' => $custom_username],
['ID' => $user_id]
);
}
Validation Messages #
Customize the ‘Email already exists’ message #
The code will change the ‘Email already exists’ message to the custom message you put. Just replace ‘Your message goes here’ with your custom message.
add_filter( 'user_registration_validate_user_email_message', 'user_registration_user_email_error_message', 11, 1 );
function user_registration_user_email_error_message( $msg ) {
if ( 'Email already exists.' === $msg ) {
return __( 'Your Error Message goes here.', 'user-registration' );
} else {
return $msg;
}
}
Change the error message during lost password #
This code will replace the error message that says ‘Invalid username or email’ with your custom message.
add_filter( 'user_registration_add_error',function( $message ){
if( 'Invalid username or email.' === $message ) {
$message = __('your custom message','user-registration');
}
return $message;
});
Change Lost Password Description #
add_filter( 'user_registration_lost_password_message', function( $msg ){
$msg = __( "Enter text","user_registration");
return $msg;
});
Redirection Specifics #
Redirect to a different page after clicking on the Email Confirmation URL #
Replace the URL with your desired URL on the code below.
add_action( 'user_registration_check_token_complete', 'ur_auto_login_email_verification', 10, 2 );
function ur_auto_login_email_verification( $user_id, $user_reg_successful ) {
if( true === $user_reg_successful ) {
wp_set_auth_cookie( $user_id );
$form_id_array = get_user_meta( $user_id, 'ur_form_id' );
$form_id = 0;
$url = '';
if ( isset( $form_id_array[0] ) ) {
$form_id = $form_id_array[0];
}
if ( '4178' === $form_id ) {
$url = "https://userregistration.dev/member/";
} else if ( '4208' === $form_id ) {
$url = "https://userregistration.dev/partner/";
}
wp_safe_redirect( $url );
die();
}
}
Redirect to Homepage after Social Login #
Use the following code to redirect the users to the Homepage once they login using social sites.
add_filter( 'user_registration_social_connect_login_redirect', 'user_registration_redirect_after_social_login' );
function user_registration_redirect_after_social_login( $redirect_url ) {
$redirect_url = home_url();
return $redirect_url;
}
WooCommerce Specifics #
Populate the first name and last name with the input given on the billing first name and last name #
add_action( 'user_registration_after_register_user_action', 'ur_update_first_last_name', 10, 3 );
function ur_update_first_last_name( $valid_form_data, $form_id, $user_id ) {
$first_name = '';
$last_name = '';
if ( isset( $valid_form_data['billing_first_name'] ) && ! empty( $valid_form_data['billing_first_name']->value ) ) {
$first_name = $valid_form_data['billing_first_name']->value;
}
if ( isset( $valid_form_data['billing_last_name'] ) && ! empty( $valid_form_data['billing_last_name']->value ) ) {
$last_name = $valid_form_data['billing_last_name']->value;
}
if ( ! empty( $first_name ) || ! empty( $last_name ) ) {
$user_data = array(
'ID' => $user_id, // this is the ID of the user you want to update.
'first_name' => $first_name,
'last_name' => $last_name,
);
wp_update_user( $user_data );
}
}
Populate the billing first name and last name with the input given on the first name and last name #
add_action( 'user_registration_after_register_user_action', 'ur_update_first_last_name', 10, 3 );
function ur_update_first_last_name( $valid_form_data, $form_id, $user_id ) {
$first_name = '';
$last_name = '';
if ( isset( $valid_form_data['first_name'] ) && ! empty( $valid_form_data['first_name']->value ) ) {
$first_name = $valid_form_data['first_name']->value;
}
if ( isset( $valid_form_data['last_name'] ) && ! empty( $valid_form_data['last_name']->value ) ) {
$last_name = $valid_form_data['last_name']->value;
}
if ( ! empty( $first_name ) ) {
update_user_meta( $user_id, 'billing_first_name', $first_name );
}
if ( ! empty( $last_name ) ) {
update_user_meta( $user_id, 'billing_last_name', $last_name );
}
}
Fields Specifics #
Limit Username character length #
The code below will limit the character length of the username field to a maximum of 15 characters only.
add_action( 'user_registration_validate_user_login', 'ur_validate_user_login_field', 10, 4 );
function ur_validate_user_login_field( $single_form_field, $data, $filter_hook, $form_id ) {
$field_label = isset( $data->label ) ? $data->label : '';
$username = isset( $data->value ) ? $data->value : '';
if ( 15 < strlen( $username ) ) {
add_filter(
$filter_hook,
function ( $msg ) use ( $field_label ) {
return __( $field_label . ' cannot exceed 15 characters.', 'user-registration' );
}
);
}
}
Remove space ‘ ‘ in the username #
This code snippet will help you remove the space/whitespace, if any, between the value entered in the username field.
add_filter( 'validate_username', 'custom_validate_username', 10, 2 );
function custom_validate_username( $valid, $username ) {
if ( preg_match( '/\\s/', $username ) ) {
there are spaces
return $valid = false;
}
return $valid;
}
Display only the specific countries on the Country Dropdown #
This code will show ‘Sweden’ and the ‘Denmark’ as the only available options on the country dropdown.
add_filter( 'user_registration_countries_list', 'display_specific_only' );
function display_specific_only( $country_list ) {
$country_list = array( '' => '--Select--', 'SE' => __( 'Sweden', 'user-registration' ), 'DK' => __( 'Denmark', 'user-registration' ) );
return $country_list;
}
For WooCommerce Billing Country Field #
add_filter( 'user_registration_billing_country_frontend_form_data', 'display_specific_country', 11 );
function display_specific_country( $data ) {
$data['form_data']['options'] = array( '' => '--Select--', 'AU' => __( 'Australia', 'user-registration' )
);
return $data;
}
Add a new file type in the File Upload field #
If you want to add a custom file type that is not available in the default settings of the File Upload field, you can use the following code. This will add file types CSV and xls.
add_filter( 'user_registration_file_upload_valid_file_type', 'urfu_add_new_valid_file_type', 10, 1 );
function urfu_add_new_valid_file_type( $validTypes ) {
$newTypes = array(
'csv' => __( 'CSV', 'user-registration-file-upload' ),
'xls' => __( 'XLS', 'user-registration-file-upload' ),
);
$validTypes = array_merge( $validTypes, $newTypes );
return $validTypes;
}
Misc #
Show ‘Log In’ when logged out and ‘My Account’ when logged in on the Primary Menu #
As the name suggests, this code will show Log In when logged out and ‘My Account’ when logged in on the primary menu of your site.
function ur_login_menu_hide( $items ) {
$login_url = 'http://wpeverest.test/test-user-registration-login/'; // Your login page url.
if ( is_user_logged_in() ) {
if ( is_array( $items ) ) {
foreach ( $items as $key => $item ) {
if ( $login_url === $item->url ) {
$items[ $key ]->title = 'Profile';
}
}
}
}
return $items;
}
add_filter( 'wp_nav_menu_objects', 'ur_login_menu_hide', 10 );
Remove the user_registration prefix from custom fields #
add_action( 'user_registration_after_register_user_action', 'create_user_type_meta', 10, 3 );
function create_user_type_meta( $form_data, $form_id, $user_id ) {
foreach($form_data as $key => $value){
$user_type = isset( $form_data[$key] ) ? $form_data[$key]->value : '';
update_user_meta( $user_id, $key, $user_type );
}
}