I’ve written a plugin to allow authentication to a WordPress install via an external API. Everything is working except the first and last names are not being set for the new WordPress user created by the plugin when a login attempt passes the external authentication.
Here’s the really strange part: one of the selectable display names is the first and last names coming in from the external API.
$userdata = array( 'user_email' => $ext_user['email'],
'user_login' => $ext_user['email'],
'first_name' => $ext_user['firstName'],
'last_name' => $ext_user['lastName'],
'role' => $user_role
);
$new_user_id = wp_insert_user( $userdata );
$user = new WP_User ($new_user_id);
That’s the code I’m using to create the new users. I have logged both $ext_user['firstName']
and $ext_user['lastName']
just before filling the $userdata
array to ensure those values are coming through properly and they are. I can’t understand how they can be in the Display Name field but the First Name and Last Name fields are blank. Can anyone help?
UPDATE: This install runs the Cimy User Extra Fields plugin. At first, I thought this might be the culprit, but I disabled it and the issue persists. Maybe that’s still the problem, but I don’t know how to determine. Could this plugin replace the default first and last name fields?
After wp_insert_user, you could try running
update_user_meta( $new_user_id, "first_name", $ext_user['firstName'] ) ;
This code works when I try it, though it generates an “undefinded variable” error (looks like you should pass a user password). However, there are a number of filters in there that could be used to manipulate the data. including
pre_user_first_name
andpre_user_last_name
. As those fields are “meta” fields, it would also be possible to alter the data via filters run byupdate_user_meta()
. That data is passed throughupdate_metadata()
which allows selective filtering.I can only assume that one or more of those filters are involved in creating this issue.