Skip to content

Instantly share code, notes, and snippets.

@phoopee3
Last active January 31, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phoopee3/69e991f08eb595efdaedb7a02bc0e8ef to your computer and use it in GitHub Desktop.
Save phoopee3/69e991f08eb595efdaedb7a02bc0e8ef to your computer and use it in GitHub Desktop.
function updateUserNicename( $user_id, $posted_field_ids, $errors, $old_values, $new_values )
{
$old_nicename = wp_cache_get( 'bp_user_username_' . $user_id, 'bp' );
if ( empty( $errors ) ) {
// update user_nicename
// field 2 = first name
// field 3 = last name
$new_nicename = $new_values[2]['value'] . '-' . $new_values[3]['value'];
// update user_nicename
$updated = wp_update_user( array( 'ID' => $user_id, 'user_nicename' => $new_nicename ) );
if ( is_wp_error( $updated ) ) {
die($updated);
} else {
$new_nicename = get_userdata( $user_id )->user_nicename;
// wp_cache_delete( 'bp_user_username_' . $user_id, 'bp' );
// wp_cache_delete( 'bp_core_userdata_' . $user_id, 'bp' );
wp_cache_replace( 'bp_user_username_' . $user_id, $new_nicename, 'bp' );
}
}
$bp = buddypress();
$bp->displayed_user->domain = str_replace( $old_nicename, $new_nicename, $bp->displayed_user->domain );
}
add_action('xprofile_updated_profile', 'updateUserNicename', 1, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment