bjornwebdesign (@bjornwebdesign)

Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)
  • @bjornwebdesign

    Participant

    Well, as you can see, the code posted here is almost 5 years old. So it’s understandable it doesn’t work anymore ;-). Also, I don’t think a 404 means it’s a “serious privacy issue”. Something isn’t working correctly, so my first step would be to check the logs and see what is causing the 404 and go from there.

    Very busy atm and currently don’t have a BP test setup running. If you still need a solution maybe I can find some time in the coming days.

    @bjornwebdesign

    Participant

    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

    @bjornwebdesign

    Participant

    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

    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

    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

    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.

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