Buddypress: Hide Name Field In Registration Page And Automatically Insert Wordpress Username In It.

Hello, I have been searching for an answer on How To Hide Name Field In Buddypress Registration Page And Automatically Insert Wordpress Username In It and I found This Topic. The guy named @aaronthomas1979 claims that he does exactly what I’m trying to do on his site, and he gives his site register.php so others could see what he has done. The problem is i don’t know where to find this command in his register.php file. Also I have wrote to live support today and one guy managed to make a code from the guy named @ositive simular to what i need, it generates username from name field, that is exactly reverse of what i need, the problem with this is that if a person writes the name with space or other unknown simbol. The username is different, and the person won't be able to login with username, so what i need to to insert the username and generate the same name from it. The code that the support staff created for me is:

add_action( 'bp_core_validate_user_signup', 'custom_validate_user_signup' );

function custom_validate_user_signup($result){

unset($result['errors']->errors['user_name']);

if(!empty($_POST['field_1']))
{
$result['user_name'] = $_POST['field_1'];
$_POST['signup_username'] = $result['user_name'];

if(!empty($_POST['field_1']))
{
$result['user_name'] = $_POST['field_1'];
$_POST['signup_username'] = $result['user_name'];
$result['user'] = $_POST['field_1'];
$_POST['signup_password_confirm']=$_POST['signup_password'];
}
return $result;
}

I’m kindly asking for your help, if you could find it in @aaronthomas1979 site or make one for me.

  • Panos

    Hi Liutauras ,

    Apologies for missing this task!

    I have managed the above with following steps:

    1st. Make the Name field not required. In a mu-plugin add the following snippet:

    add_filter( 'xprofile_field_is_required_before_save', function( $is_required, $field_id ){
    
    	if( 1 == $field_id ){
    		return false;
    	}
    	return $is_required;
    }, 20, 2 );

    Then you will need to re-save the name field so it's set as not required.

    Of what I know the id of the name field is always "1" unless some custom job has changed this. You can be sure by clicking on editing the name field, it should mention the field_id on the url. In case it is different replace the appropriate id here:
    if( 1 == $field_id ){

    2nd. We can hide it with some css. You can use the mu-plugin again if you like, of any theme or plugin that allows custom css:

    add_action( 'wp_head', function(){
    	?>
    	<style type="text/css">
    		#profile-details-section div.field_name{
    			display:none;
    		}
    	</style>
    	<?php
    
    }, 10 );

    3rd. Since the name field is empty, the username will be used by default. In case you need to modify more you can try the following:

    add_action( 'bp_core_signup_user', function( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
    
    	global $wpdb;
    	$new_name = $user_login;
    	$usermeta['field_1'] = $new_name;
    
    	$wpdb->update(
    			buddypress()->members->table_name_signups,
    			array(
    				'meta' => serialize( $usermeta ),
    			),
    			array(
    				'user_login' => $user_login,
    			),
    			array(
    				'%s',
    			),
    			array(
    				'%s',
    			)
    		);
    
    	update_user_meta( $user_id, 'nickname', $new_name );
    	wp_update_user( array( 'ID' => $user_id, 'user_nicename' => $new_name, 'display_name' => $new_name ) );
    
    }, 20, 5 );

    and change line
    $new_name = $user_login;
    accordingly.

    Hope this helps!
    Cheers!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.