Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Autogenerate or remove username


peterverkooijen
Participant

@peterverkooijen

This function for bp-custom.php adds fullname from xprofile field_1 to wp_usermeta.

function synchro_wp_usermeta($user_id, $password, $meta) {
global $bp, $wpdb;

$fullname = $meta[field_1];
$space = strpos( $fullname, ' ' );

if ( false === $space ) {
$firstname = $fullname;
$lastname = '';
} else {
$firstname = substr( $fullname, 0, $space );
$lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
}

update_usermeta( $user_id, 'nickname', $fullname );
update_usermeta( $user_id, 'first_name', $firstname );
update_usermeta( $user_id, 'last_name', $lastname );

$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $user_id ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $user_id ), $user_id ) );
}
add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);

It shouldn’t be a big step to make the fullname lowercase, strip the spaces and store it as username.

Make lowercase and strip space looks something like this:

$str = strtolower($str);

$str = str_replace(' ', '', $str);

But does the system even allow changing the username at the wpmu_activate_user stage?

The username is stored in four places in the database:

wp_signups -> user_login

wp_users -> user_login

wp_users -> user_nicename (?!)

wp_users -> user_url (as last part)

Manually changing the username at these points in the database does NOT screw up my system. Login with email address continues to work fine with the same password. The profile page and links function as normal. :-)

That is good news. It means it is possible to simply update the username from this same function.

Now I just have to figure out the PHP and the queries for those four fields. Any pointers apprecriated!

EDIT: Is $user_id a number or the username? That could complicate things…

I assume $user_id is the username. Can I make this a two-stage rocket? First run the synchro_wp_usermeta function and then a separate function to deal with username, synchro_name_username? Put them in a plugin with something like this at the bottom?:

add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);
add_action( 'wpmu_activate_user', 'synchro_name_username');

Would that execute the functions in the right order?

Skip to toolbar