Reply To: User Profile Url uses the username. Can that be changed?
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