Skip to:
Content
Pages
Categories
Search
Top
Bottom

User Profile Url uses the username. Can that be changed?


  • J
    Participant

    @paradox_designs

    Hi,

    Using bbP 2.3.2 on WP 3.6.

    This may seem silly but isn’t using a user’s username (aka their login username) in their public profile url a reduction in security?

    In any case, I want to see if there is any possibility to change that. the main reason is that for other reasons we have used people’s email addresses as the usernames when creating accounts. This made some sense at the time but provides an issue where now that we have added bbPress, people’s profile url includes their email address. something most of them will probably not like, and is reduction in security and privacy.

    Is there a way to make it something like http://www.sitename.com/users/firstname_lastname/ instead?

    or is there any other ideas as how to solve this dilemma?

    Thanks

Viewing 14 replies - 1 through 14 (of 14 total)

  • J
    Participant

    @paradox_designs

    After 2 months I have resolved this. if anyone else needs help just ask


    dotdatdot
    Participant

    @dotdatdot

    Please do share your solution here as I agree with this being an issue. Thanks in advance!


    J
    Participant

    @paradox_designs

    
    add_action( 'init', 'nicenames_to_display_name' );
    function nicenames_to_display_name() {
        foreach ( get_users() as $user ) {
    		if ( $user->data->user_status == 0 && $user->data->user_nicename != $user->data->display_name ) {
    			$user_ids[] = $user->ID;
    		}
    	}
    	foreach( $user_ids as $uid ) {
    		$info = get_userdata( $uid );
    		$display_name = $info->data->display_name;
    		if ($display_name) {
    			$args = array(
    				'ID'            => $uid,
    				'user_nicename' => strtolower(str_replace(" ", "_", $display_name))
    			);
    			wp_update_user( $args );
    		}
    	}
    }
    

    Jgonl
    Participant

    @jgonl

    Where to put the code?


    Jgonl
    Participant

    @jgonl

    Thanks … ok


    J
    Participant

    @paradox_designs

    Damn, sorry. i hate when others do it and I end up doing it myself…

    This goes in functions.php


    Jgonl
    Participant

    @jgonl

    I figured I should go where the code and may assert.
    Thank you again


    fatsandrew7
    Participant

    @fatsandrew7

    wow! exactly what i was looking for!

    thanks so much!


    bjornwebdesign
    Participant

    @bjornwebdesign

    Thanks for sharing this! WHAT was BP thinking when they decided to use the username as profile url… I know why (only mandatory userdata on register), but imho the first thing to develop next is a solution to change this..

    EDIT

    When implementing your solution i found something.
    Doing $user->data->user_nicename != $user->data->display_name in the first foreach loop will not work because when updating (second loop) the user_nicename you do a strtolower and str_replace.
    If you want the check to work use the same conversion on the first loop.


    bjornwebdesign
    Participant

    @bjornwebdesign

    Wanted to share my finished code:

    // Change the user_nicename which is used as profile url, we do NOT want the username in url! Bad bad BP...
    // Wheb using the 'init' action hook this will always run, we should only do this once and then on user register & profile update..
    // Please note: the user profile url can now be changed by the user, direct linking from other places on the web may result in 404.
    // And offcourse allways use something like: 'bp_core_get_user_domain( $user_id )' when you want to get the user's profile url.
    $search = array( ' ', '.' ); 
    $replace = array( '_', '' );		
    foreach ( get_users() as $user ) {
    	if ( $user->data->user_status == 0 && $user->data->user_nicename != strtolower(str_replace( $search, $replace, $user->data->display_name)) ) {
    		$user_ids[] = $user->ID;
    	}
    }
    foreach( $user_ids as $uid ) {
    	$user_data = get_userdata( $uid );
    	$display_name = $user_data->data->display_name;
    	if ($display_name) {
    		$args = array(
    			'ID'            => $uid,
    			'user_nicename' => strtolower(str_replace( $search, $replace, $display_name))
    		);
    		wp_update_user( $args );
    	}
    }

    bjornwebdesign
    Participant

    @bjornwebdesign

    LoL, I thought i was finished, but i noticed the second loop isn’t necessary.
    So here’s my finished/tested code:

    /*
     * Change the user_nicename which is used as profile url, we do NOT want the username in url! Bad bad BP...
     * This conversion runs allways, we should only do this once and then on user register & profile update..
     * Please note: the user profile url can now be changed by the user, direct linking from other places on the web may result in 404.
     * And offcourse allways use something like: 'bp_core_get_user_domain( $user_id )' when you want to get the user's profile url.
     */
    $search = array( ' ', '.' ); 
    $replace = array( '_', '' );
    $all_users = get_users();		
    foreach ( $all_users as $user ) {
    	$display_name = $user->data->display_name;
    	if ( $user->data->user_status == 0 && $display_name ) {
    		$new_user_nicename = strtolower(str_replace( $search, $replace, $display_name) );
    		if ( $user->data->user_nicename != $new_user_nicename ) {
    				$args = array(
    					'ID'            => $user->ID,
    					'user_nicename' => $new_user_nicename
    				);
    				wp_update_user( $args );					
    		}
    	}
    }

    Put this in a function and call it on an action hook, can be done in your theme’s functions.php just like the OP already explained here.

    The function will change something like this: ‘Mr. A.T. Testing123’ into ‘mr_at_testing123’.


    bjornwebdesign
    Participant

    @bjornwebdesign

    OMG… We webdesigners are never finished 😛

    After some more testing I noticed that the function did not change all user_nicename.
    The DB type of user_nicename = VARCHAR(50) and the type of display_name = VARCHAR(250).
    If the updated user_nicename exceeds 50 chars the DB field will not update and nothing changes. So I added a substr to resolve this.
    Thankfully the wp_update_user() takes care off special characters like ë.

    Updated code:

    /*
     * Change the user_nicename which is used as profile url, we do NOT want the username in url! Bad bad BP...
     * This runs allways (with init hook), we should only do this once and then on user register & profile update..
     * Please note: the user profile url can now be changed by the user, direct linking from other places on the web may result in 404.
     * And offcourse allways use something like: 'bp_core_get_user_domain( $user_id )' when you want to get the user's profile url.
     */
    $search = array( ' ', '.' ); 
    $replace = array( '_', '' );
    $all_users = get_users();		
    foreach ( $all_users as $user ) {
    	$display_name = $user->data->display_name;
    	if ( $user->data->user_status == 0 && $display_name ) {
    		$new_user_nicename = strtolower(str_replace( $search, $replace, $display_name) );
    		if ( strlen ( $new_user_nicename ) > 50 ) {
    			$new_user_nicename = substr ( $new_user_nicename, 0, 50 );
    		}				
    		if ( $user->data->user_nicename != $new_user_nicename ) { // && $user->ID == 80 <-Add this if you only want to run it for 1 user, so you can test it.
    			$args = array(
    				'ID'            => $user->ID,
    				'user_nicename' => $new_user_nicename
    			);
    			wp_update_user( $args );					
    		}
    	}
    }

    bjornwebdesign
    Participant

    @bjornwebdesign

    Ok, last time, I promise :p…

    I updated all my users with the above function, but I don’t want it to run every time on the init hook.
    So I changed it into:

    /*
     * Do stuff when the user's xprofile is updated
    */	
    function xprofile_updated ( $user_id, $posted_field_ids, $errors, $old_values, $new_values) {
    	/*
    	 * Change the user_nicename which is used as profile url, we do NOT want the username in url! Bad bad BP...
    	 * Please note: the user profile url can now be changed by the user, direct linking from other places on the web may result in 404.
    	 * Altough this should run AFTER updating profile fields (saving to DB), the nicename is only updated after a second save. So we need to check from $new_values
    	 */
    	$new_display_name = '';
    	foreach ( $new_values as $key => $value ) {
    		if ( is_array($value) && $key == 1 ) { // field display_name = 1, make sure this is correct
    			foreach ( $value as $k => $v ) {
    				if ( $k == 'value' ) {
    					$new_display_name = $v;
    				}
    			}
    		}
    	}
    	//error_log('******** xprofile_updated: '.$user_id.' | NEW DISPLAY_NAME: '.$new_display_name.' *********');
    	$search = array( ' ', '.' ); 
    	$replace = array( '_', '' );
    	$user = get_user_by( 'ID', $user_id );		
    	if ( $user ) {
    		if ( $user->data->user_status == 0 && $new_display_name ) {
    			$new_user_nicename = strtolower(str_replace( $search, $replace, $new_display_name) );
    			if ( strlen ( $new_user_nicename ) > 50 ) {
    				$new_user_nicename = substr ( $new_user_nicename, 0, 50 );
    			}				
    			if ( $user->data->user_nicename != $new_user_nicename ) { // && $user->ID == 80 <-Add this if you only want to run it for 1 user, so you can test it.
    				$args = array(
    					'ID'            => $user->ID,
    					'user_nicename' => $new_user_nicename
    				);
    				wp_update_user( $args );
    				//error_log('******** updated user_nicename: '.$user->ID.' | NEW USER_NICENAME: '.$new_user_nicename.' *********');
    				wp_redirect( get_site_url().'/leden/'.$new_user_nicename.'/profile/edit/group/1/' ); // we cant use bp_core_get_user_domain() here, because it still uses the old user_nicename
    				exit;					
    			}
    		}
    	}
    }
    add_action( 'xprofile_updated_profile',  'xprofile_updated', 100, 5 );

    Please note, it has some site specific code, like the wp_redirect.
    Any questions? Feel free to ask.

    @mod: Hoping my code snippet is not too long.

    Regards, Bjorn


    Pascal Casier
    Moderator

    @casiepa

    @bjornwebdesign,
    Your snippets are not that long, so that’s ok, just the number of snippets is maybe too high 🙂
    My only remark is you’re based on a BuddyPress function and this is a bbPress forum. But still useful anyway!
    Thanks,
    Pascal.

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.
Skip to toolbar