Reputation: 1447
I'm trying to set the display name of the user on registration to be their first name.
The following function works but only when a user logs in, not when they first register (if they log out and log in again then this takes effect):
function force_pretty_displaynames($user_login, $user) {
$outcome = trim(get_user_meta($user->ID, 'first_name', true));
if (!empty($outcome) && ($user->data->display_name!=$outcome)) {
wp_update_user( array ('ID' => $user->ID, 'display_name' => $outcome));
}
}
add_action('wp_login','force_pretty_displaynames',10,2);
If I change the hook from wp_login
to user_register
it gives an error due to too few arguments:
function force_pretty_displaynames($user_login, $user) {
$outcome = trim(get_user_meta($user->ID, 'first_name', true));
if (!empty($outcome) && ($user->data->display_name!=$outcome)) {
wp_update_user( array ('ID' => $user->ID, 'display_name' => $outcome));
}
}
add_action('user_register','force_pretty_displaynames',10,2);
So if I remove one of the arguments as below, it doesn't error but also doesn't work either:
function force_pretty_displaynames($user) {
$outcome = trim(get_user_meta($user->ID, 'first_name', true));
if (!empty($outcome) && ($user->data->display_name!=$outcome)) {
wp_update_user( array ('ID' => $user->ID, 'display_name' => $outcome));
}
}
add_action('user_register','force_pretty_displaynames',10,2);
How can I set the display name on registration to be the first name?
I have this function to set the username to the full email address on registration, can I add it in to this somehow?
add_filter( 'pre_user_login' , 'cfw_set_username_to_email' );
function cfw_set_username_to_email( $user_login ) {
if( isset($_POST['billing_email'] ) ) {
$user_login = $_POST['billing_email'];
}
if( isset($_POST['email'] ) ) {
$user_login = $_POST['email'];
}
return $user_login;
}
Upvotes: 0
Views: 2249
Reputation: 1
This Woocommerce filter gets the first or the second array before Wordpress function wp_insert_user() is called. This wil create/insert a new user. Update user is done by wp_update_user().
/*
$data1 = array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => 'customer',
)
$data2 = array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $user_email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'customer',
'source' => 'store-api,',
)
*/
apply_filters( 'woocommerce_new_customer_data', 'create_user_display_name' );
function create_user_display_name( $data ){
if( array_key_exists( 'role', $data ) && $data['role'] === 'customer' ){
$display_name = '';
if( !array_key_exists( 'display_name' , $data ) ) {
if( array_key_exists( 'first_name' , $data ) ) {
$display_name = $data['first_name'];
}
else if(array_key_exists( 'user_email' , $data ) ) {
$display_name = $data['user_email'];
}
}else{
$display_name = $data['display_name'];
}
if( strpos( $display_name , '@' ) > 0 ) {
$display_arr = explode( '@' , $display_name );
$args['display_name'] = $display_arr[0];
}
}
return $data;
}
Upvotes: 0
Reputation: 142
add_filter( 'pre_user_display_name', 'set_display_name_to_forename' );
This line adds a filter to the pre_user_display_name hook. Filters in WordPress allow you to modify data before it's used or saved. In this case, the code specifies that the set_display_name_to_forename
function should be applied to modify the display name before it's saved.
function set_display_name_to_forename( $display_name ) {
if ( isset( $_POST['billing_first_name'] ) ) {
$display_name = sanitize_text_field( $_POST['billing_first_name'] );
} elseif ( isset( $_POST['first_name'] ) ) {
$display_name = sanitize_text_field( $_POST['first_name'] );
}
return $display_name;
}
Here's the breakdown of the set_display_name_to_forename
function:
if ( isset( $_POST['billing_first_name'] ) ) { ... }
This condition checks if the billing_first_name field is present in the $_POST array. This field is commonly used in WooCommerce registration forms to collect the user's first name.if ( isset( $_POST['first_name'] ) ) { ... }
If the billing_first_name
field is not present, this condition checks if the first_name
field is present in the $_POST
array. The first_name
field is used in the standard WordPress registration form to collect the user's first name.sanitize_text_field()
to ensure that any potentially harmful input is sanitized. It sets the display name to the value of either billing_first_name
or first_name
based on which field is present.Overall, this code takes advantage of the WordPress filter system to intercept the display name before it's saved and dynamically replaces it with the user's provided first name from the registration form. It's a neat way to personalize the display name and create a more user-friendly experience for your WooCommerce registration process.
Upvotes: 0
Reputation: 1447
I found the solution - there's another hook pre_user_display_name
which does exactly what I needed, using a similar function to the one I already have for pre_user_login
add_filter( 'pre_user_display_name' , 'cfw_set_display_name_to_forename' );
function cfw_set_display_name_to_forename( $display_name ) {
if( isset($_POST['billing_first_name'] ) ) {
$display_name = $_POST['billing_first_name'];
}
if( isset($_POST['first_name'] ) ) {
$display_name = $_POST['first_name'];
}
return $display_name;
}
Tested and works!
Upvotes: 4